(Newbie) Stuck with annotate() docs

26 vistas
Ir al primer mensaje no leído

Ankush Thakur

no leída,
26 abr 2016, 12:50:31 p.m.26/4/2016
para Django users
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.

Tim Graham

no leída,
26 abr 2016, 7:06:40 p.m.26/4/2016
para Django users
Looks like a typo. Does the rest of the example make sense and work if that line is changed to "Book.objects.first().chapters"?

Ankush Thakur

no leída,
27 abr 2016, 10:45:03 a.m.27/4/2016
para Django users
I'm not sure how Book.objects.first().chapters is different from Book.objects.first().chapters.count(), but the point is that "chapters" is not defined in the models at the top of that page! The snippet makes sense if I treat Book.chapters as another ManyToManyField (like authors), but it's really weird to see that error in the docs. So, does that mean I can report it? If yes, to who?

Tim Graham

no leída,
27 abr 2016, 2:51:35 p.m.27/4/2016
para Django users

Ankush Thakur

no leída,
28 abr 2016, 12:51:05 p.m.28/4/2016
para Django users
Yup! Makes sense now. I hope it saves someone some trouble someday. :-)

Thanks a lot, Tim!

Best,
Ankush
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos