django-generic-aggregation Documentation

Similar documents
django-renderit Documentation

django-conduit Documentation

redis-completion Documentation

Querying Data with Transact SQL

Bricks Documentation. Release 1.0. Germano Guerrini

django-audit-log Documentation

Django REST Framework JSON API Documentation

Django ORM crash

Nested Queries. Dr Paolo Guagliardo. Aggregate results in WHERE The right way. Fall 2018

Bambu API Documentation

django-model-utils Documentation

django-auditlog Documentation

TPS Documentation. Release Thomas Roten

django-autocomplete-light Documentation

django-autocomplete-light Documentation

Poetaster. Release 0.1.1

Django Admin Sortable Documentation

MTA Database Administrator Fundamentals Course

T-SQL Training: T-SQL for SQL Server for Developers

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Django IPRestrict Documentation

SQLAlchemy-ORM-tree Documentation

Python Schema Generator Documentation

Django-Select2 Documentation. Nirupam Biswas

django CMS Export Objects Documentation

Writing Analytical Queries for Business Intelligence

YouTube API Wrapper Documentation

MySQL for Developers with Developer Techniques Accelerated

chatterbot-weather Documentation

Django-CSP Documentation

django-cron Documentation

Roman Numeral Converter Documentation

SQL functions fit into two broad categories: Data definition language Data manipulation language

MIT Global Startup Labs México 2013

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Advanced Oracle Performance Troubleshooting. Query Transformations Randolf Geist

This course is aimed at those who need to extract information from a relational database system.

django-composite-foreignkey Documentation

Relational Database Management Systems for Epidemiologists: SQL Part II

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

PowerPoint Presentation to Accompany GO! All In One. Chapter 13

django-slim Documentation

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Release Fulfil.IO Inc.

Django Synctool Documentation

3. Getting data out: database queries. Querying

Python wrapper for Viscosity.app Documentation

django-composite-foreignkey Documentation

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Django-frontend-notification Documentation

Python Utils Documentation

Sql Server Schema Update Join Multiple Tables In One Query

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Python Project Example Documentation

Django Map Widgets Documentation

tally Documentation Release Dan Watson

COMP 330: SQL 3. Chris Jermaine and Kia Teymourian Rice University

CE419 Web Programming. Session 15: Django Web Framework

The SQL Guide to Pervasive PSQL. Rick F. van der Lans

Subquery: There are basically three types of subqueries are:

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Querying Data with Transact-SQL

pyldavis Documentation

OVERVIEW OF RELATIONAL DATABASES: KEYS

MongoDB Step By Step. By B.A.Khivsara Assistant Professor Department of Computer Engineering SNJB s COE,Chandwad

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

django simple pagination Documentation

wagtail-robots Documentation

oemof.db Documentation

Bishop Blanchet Intranet Documentation

Redis Timeseries Documentation

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Python simple arp table reader Documentation

COPYRIGHTED MATERIAL. Contents. Chapter 1: Introducing T-SQL and Data Management Systems 1. Chapter 2: SQL Server Fundamentals 23.

Querying Data with Transact-SQL (761)

Nirvana Documentation

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

MySQL for Developers. Duration: 5 Days

MySQL for Developers. Duration: 5 Days

Sql Server Syllabus. Overview

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Pulp Python Support Documentation

PrettyPandas Documentation

Scout Documentation. Release Charles Leifer

SQL Interview Questions

COGNOS (R) 8 GUIDELINES FOR MODELING METADATA FRAMEWORK MANAGER. Cognos(R) 8 Business Intelligence Readme Guidelines for Modeling Metadata

Mobile MOUSe MTA DATABASE ADMINISTRATOR FUNDAMENTALS ONLINE COURSE OUTLINE

Ansible Tower API Guide

kiss.py Documentation

PyCRC Documentation. Release 1.0

django-idioticon Documentation

Aircrack-ng python bindings Documentation

Course Outline. MySQL Database Administration & Design. Course Description: Pre-requisites: Course Content:

withenv Documentation

Oracle Syllabus Course code-r10605 SQL

Connecting Spotfire to Data Sources with Information Designer

Python Utils Documentation

SQL for MySQL A Beginner s Tutorial

Django Graphos Documentation

Transcription:

django-generic-aggregation Documentation Release 0.4.0 charles leifer September 20, 2016

Contents 1 installation 3 2 examples 5 3 important detail 7 4 api 9 4.1 Indices and tables............................................ 10 Python Module Index 11 i

ii

django-generic-aggregation Documentation, Release 0.4.0 annotate() and aggregate() for generically-related data. also a handy function for filtering GFK-model querysets. Note: Use django s GenericRelation where possible, as this can make the queries generated more efficient by using a JOIN rather than a subquery. Contents 1

django-generic-aggregation Documentation, Release 0.4.0 2 Contents

CHAPTER 1 installation # install from pypi pip install django-generic-aggregation # or install via git pip install -e git+git://github.com/coleifer/django-generic-aggregation.git#egg=generic_aggregation 3

django-generic-aggregation Documentation, Release 0.4.0 4 Chapter 1. installation

CHAPTER 2 examples The examples below assume the following simple models: class Rating(models.Model): rating = models.integerfield() object_id = models.integerfield() content_type = models.foreignkey(contenttype) content_object = GenericForeignKey(ct_field='content_type', fk_field='object_id') class Food(models.Model): name = models.charfield(max_length=50) ratings = GenericRelation(Rating) # reverse generic relation You want to figure out which items are highest rated (generic_annotate()) from django.db.models import Avg food_qs = Food.objects.filter(name startswith='a') generic_annotate(food_qs, Rating, Avg('ratings rating')) # you can mix and match queryset / model generic_annotate(food_qs, Rating.objects.all(), Avg('ratings rating')) You want the average rating for all foods that start with a (generic_aggregate()) food_qs = Food.objects.filter(name startswith='a') generic_aggregate(food_qs, Rating, Avg('ratings rating')) You want to only display ratings for foods that start with a (generic_filter()) food_qs = Food.objects.filter(name startswith= a ) generic_filter(rating.objects.all(), food_qs) 5

django-generic-aggregation Documentation, Release 0.4.0 6 Chapter 2. examples

CHAPTER 3 important detail As you may have noted in the above examples (at least those using annotate and aggregate), the aggregate we pass in is prefixed with ratings. The double-underscore prefix refers to the ratings attribute of the Food model, which is a django.contrib.contenttypes.fields.genericrelation instance. We are querying across that relation to the field on the Ratings model that we are interested in. When possible, use a GenericRelation and construct your queries in this manner. If you do not have a GenericRelation on the model being queried, it will use a fallback method that will return the correct results, though queried in a slightly different manner (a subquery will be used as opposed to a left outer join). If for some reason the Generic Foreign Key s object_id field is of a different type than the Primary Key of the related model which is probably the case if you re using django.contrib.comments, as it uses a TextField a CAST expression is required by some RDBMS. Django will not put it there for you, so again, the code will use the fallback methods in this case, which add the necessary CAST. View the code for the nitty-gritty details. 7

django-generic-aggregation Documentation, Release 0.4.0 8 Chapter 3. important detail

CHAPTER 4 api generic_aggregation.generic_annotate(qs_model, generic_qs_model, aggregator[, gfk_field=none[, alias= score ]]) Find blog entries with the most comments: qs = generic_annotate(entry.objects.public(), Comment.objects.public(), Count('comments id')) for entry in qs: print entry.title, entry.score Find the highest rated foods: generic_annotate(food, Rating, Avg('ratings rating'), alias='avg') for food in qs: print food.name, '- average rating:', food.avg Note: In both of the above examples it is assumed that a GenericRelation exists on Entry to Comment (named comments ) and also on Food to Rating (named ratings ). If a GenericRelation does not exist, the query will still return correct results but the code path will be different as it will use the fallback method. Warning: If the underlying column type differs between the qs_model s primary key and the generic_qs_model s foreign key column, it will use the fallback method, which can correctly CASTself. Parameters qs_model A model or a queryset of objects you want to perform annotation on, e.g. blog entries generic_qs_model A model or queryset containing a GFK, e.g. comments aggregator an aggregation, from django.db.models, e.g. Count( id ) or Avg( rating ) gfk_field explicitly specify the field w/the gfk alias attribute name to use for annotation Return type a queryset containing annotate rows generic_aggregation.generic_aggregate(qs_model, generic_qs_model, aggregator[, gfk_field=none]) Find total number of comments on blog entries: generic_aggregate(entry.objects.public(), Comment.objects.public(), Count('comments id')) Find the average rating for foods starting with a : 9

django-generic-aggregation Documentation, Release 0.4.0 a_foods = Food.objects.filter(name startswith='a') generic_aggregate(a_foods, Rating, Avg('ratings rating')) Note: In both of the above examples it is assumed that a GenericRelation exists on Entry to Comment (named comments ) and also on Food to Rating (named ratings ). If a GenericRelation does not exist, the query will still return correct results but the code path will be different as it will use the fallback method. Warning: If the underlying column type differs between the qs_model s primary key and the generic_qs_model s foreign key column, it will use the fallback method, which can correctly CASTself. Parameters qs_model A model or a queryset of objects you want to perform annotation on, e.g. blog entries generic_qs_model A model or queryset containing a GFK, e.g. comments aggregator an aggregation, from django.db.models, e.g. Count( id ) or Avg( rating ) gfk_field explicitly specify the field w/the gfk Return type a scalar value indicating the result of the aggregation generic_aggregation.generic_filter(generic_qs_model, filter_qs_model[, gfk_field=none]) Only show me ratings made on foods that start with a : a_foods = Food.objects.filter(name startswith= a ) generic_filter(rating.objects.all(), a_foods) Only show me comments from entries that are marked as public: generic_filter(comment.objects.public(), Entry.objects.public()) Parameters generic_qs_model A model or queryset containing a GFK, e.g. comments qs_model A model or a queryset of objects you want to restrict the generic_qs to gfk_field explicitly specify the field w/the gfk Return type a filtered queryset 4.1 Indices and tables genindex modindex search 10 Chapter 4. api

Python Module Index g generic_aggregation, 9 11

django-generic-aggregation Documentation, Release 0.4.0 12 Python Module Index

Index G generic_aggregate() (in module generic_aggregation), 9 generic_aggregation (module), 9 generic_annotate() (in module generic_aggregation), 9 generic_filter() (in module generic_aggregation), 10 13