Ticket #10035: 10035.diff

File 10035.diff, 1.7 KB (added by isagalaev, 7 years ago)

Patch

  • docs/topics/db/aggregation.txt

     
    3939       rating = models.FloatField()
    4040       authors = models.ManyToManyField(Author)
    4141       publisher = models.ForeignKey(Publisher)
    42        pubdate = models.DateField
     42       pubdate = models.DateField()
    4343
    4444    class Store(models.Model):
    4545       name = models.CharField(max_length=300)
     
    268268For example, consider an author query that attempts to find out the average
    269269rating of books written by each author:
    270270
    271     >>> Author.objects.annotate(average_rating=Avg('book_rating'))
     271    >>> Author.objects.annotate(average_rating=Avg('book__rating'))
    272272
    273273This will return one result for each author in the database, annotate with
    274274their average book rating.
    275275
    276276However, the result will be slightly different if you use a ``values()`` clause::
    277277
    278     >>> Author.objects.values('name').annotate(average_rating=Avg('book_rating'))
     278    >>> Author.objects.values('name').annotate(average_rating=Avg('book__rating'))
    279279
    280280In this example, the authors will be grouped by name, so you will only get
    281281an annotated result for each *unique* author name. This means if you have
     
    302302For example, if we reverse the order of the ``values()`` and ``annotate()``
    303303clause from our previous example::
    304304
    305     >>> Author.objects.annotate(average_rating=Avg('book_rating')).values('name')
     305    >>> Author.objects.annotate(average_rating=Avg('book__rating')).values('name')
    306306
    307307This will now yield one unique result for each author; however, only
    308308the author's name and the ``average_rating`` annotation will be returned
Back to Top