From: "Russell Keith-Magee" <freakboy3...@gmail.com>
Date: Tue, 5 Dec 2006 19:56:42 +0800
Local: Tues, Dec 5 2006 6:56 am
Subject: Re: Re: Suggestion: Aggregate/Grouping/Calculated methods in Django ORM
On 12/4/06, Jacob Kaplan-Moss <ja...@jacobian.org> wrote:
> I'm taking this to django-dev for more discussion; it'll get seen by more the > Thoughts, anyone? 1. Introduction class Book(Model): class Order(Model): Simple aggregation use cases would be questions like: These questions require that you query the database to obtain a single However, the more general class of problem is to generate a summary e) What is the total cost of _each_ order? IMHO, what we need is the ability to annotate summary statistics onto 2. Proposal annotate() returns a query set, so it can be used multiple times, be Model.objects.annotate(field1__field2__aggregate='annotation') field1__field2 describes the path to get to the field that will be The objects that are returned when the queryset is executed will be e.g., >>> order = Order.objects.get(id=1234).annotate( books__price__sum='total_cost', books__count='item_count') # Inspect the order >>> order.description "Dad's birthday" # Annotated orders have a 'total_cost' attribute... >>> order.total_cost 47.2 # ... and an 'item_count' attribute >>> order.item_count 3 3. Just the facts, Ma'am >>> Book.objects.aggregates(price__min='min_price', pub_date__max='last_update') {'min_price':0.5, 'last_update': 2006-11-22} aggregates() would expand queries in the same manner as annotate(), This is a more verbose notation than the simple 'max()/min()' . I have 4. Comparisons # Annotate a query set with the average price of books >>> qs = Book.objects.annotate(price__average='avg_price'). # Filter all books with obj.avg_price < obj.price >>> expensive_books = qs.filter(avg_price__lt=F('price')) The F() object is a placeholder to let the query language know that 'price' isn't just a string, it's the name of a field. This follows the example of Q() objects providing query wrappers. This capability would also be beneficial to the query language in 5. Implementation If there is an annotate clause in a query set: 6. Limitations/Problems - I'm not overly enamoured with the name aggregates() - it isn't a - I haven't tried to implement this, so there are probably some sharp ~~~~~~~~~~~~~~~~~~~~~~~ So - that's my two bits. Comments? Yours, You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||