Tuesday, April 26, 2016

(Newbie) Stuck with annotate() docs

Folks, I'm having exceptional trouble understanding annotate(), aggregate(), and their various combinations. I'm currently stuck here: https://docs.djangoproject.com/en/1.9/topics/db/aggregation/#combining-multiple-aggregations

The example here uses Book.objects.first().chapters.count(), but there's no chapters model or field at the start of the tutorial. It's frustrating, to say the least. Even if I set up a separate application to test this myself, what do I make of "chapters"? Is it another model with many-to-many relation with Book? When I ran an example with the following models:

class Author(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()

class Book(models.Model):
    name = models.CharField(max_length=300)
    chapters = models.IntegerField()
    authors = models.ManyToManyField(Author)

I got: 

>>> Book.objects.first().chapters.count()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'int' object has no attribute 'count'
>>> 

So basically, I feel like I'm screwed. Please help.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ea169a1e-d55a-457c-b0bd-19d6d96626d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment