Ticket #10035: 10035.diff
File 10035.diff, 1.7 KB (added by , 16 years ago) |
---|
-
docs/topics/db/aggregation.txt
39 39 rating = models.FloatField() 40 40 authors = models.ManyToManyField(Author) 41 41 publisher = models.ForeignKey(Publisher) 42 pubdate = models.DateField 42 pubdate = models.DateField() 43 43 44 44 class Store(models.Model): 45 45 name = models.CharField(max_length=300) … … 268 268 For example, consider an author query that attempts to find out the average 269 269 rating of books written by each author: 270 270 271 >>> Author.objects.annotate(average_rating=Avg('book_ rating'))271 >>> Author.objects.annotate(average_rating=Avg('book__rating')) 272 272 273 273 This will return one result for each author in the database, annotate with 274 274 their average book rating. 275 275 276 276 However, the result will be slightly different if you use a ``values()`` clause:: 277 277 278 >>> Author.objects.values('name').annotate(average_rating=Avg('book_ rating'))278 >>> Author.objects.values('name').annotate(average_rating=Avg('book__rating')) 279 279 280 280 In this example, the authors will be grouped by name, so you will only get 281 281 an annotated result for each *unique* author name. This means if you have … … 302 302 For example, if we reverse the order of the ``values()`` and ``annotate()`` 303 303 clause from our previous example:: 304 304 305 >>> Author.objects.annotate(average_rating=Avg('book_ rating')).values('name')305 >>> Author.objects.annotate(average_rating=Avg('book__rating')).values('name') 306 306 307 307 This will now yield one unique result for each author; however, only 308 308 the author's name and the ``average_rating`` annotation will be returned