#14476 closed Bug (fixed)
annotate, default aggregation naming and filter annoyance
Reported by: | dirleyrls | Owned by: | Ramiro Morales |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | annotate, FieldError |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Going straight to the point, if I do the following:
from django.db import models ... class Answer(models.Model): entry = models.ForeignKey(Question) ... qsa = Question.objects.annotate(answer_count=models.Count('answer')).filter(answer_count=0) qsb = Question.objects.annotate(models.Count('answer')).filter(answer__count=0)
qsa will be correctly evaluted, but qsb will raise a "FieldError: Cannot resolve keyword 'count' into field. Choices are ... answer_ _count".
Now, isn't this annoying? The error message is lying!
Attachments (2)
Change History (11)
by , 14 years ago
comment:1 by , 14 years ago
Has patch: | set |
---|
comment:2 by , 14 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
There's a slight problem here in that filter(answercount) could be referring to a default aggregate alias, or a field called count on the answer model. However, I think we can live with that ambiguity.
Patch requires tests.
comment:3 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:5 by , 13 years ago
Easy pickings: | unset |
---|---|
Resolution: | fixed |
Status: | closed → reopened |
UI/UX: | unset |
This is still a problem for aggregates.
>>> from django.contrib.auth.models import * >>> from django.db.models import * >>> User.objects.annotate(xyz=Count('groups')).aggregate(Max('xyz')) {'xyz__max': 13} >>> User.objects.annotate(Count('groups')).aggregate(Max('groups__count')) Traceback (most recent call last): File "<console>", line 1, in <module> File "/Users/mrmachine/django/db/models/query.py", line 331, in aggregate is_summary=True) File "/Users/mrmachine/django/db/models/sql/query.py", line 1000, in add_aggregate field_list, opts, self.get_initial_alias(), False) File "/Users/mrmachine/django/db/models/sql/query.py", line 1288, in setup_joins "Choices are: %s" % (name, ", ".join(names))) FieldError: Cannot resolve keyword 'count' into field. Choices are: id, name, permissions, user, groups__count
exclude()
, filter()
, values()
, values_list()
and order_by()
all work, so I assume that aggregate()
should as well.
Can we apply the same fix? Let me know if I should open a new ticket instead of re-opening this one.
Patch with updated test attached.
by , 13 years ago
Attachment: | 14476-annotate-aggregate-r17389.diff added |
---|
comment:6 by , 12 years ago
Component: | ORM aggregation → Database layer (models, ORM) |
---|---|
Version: | 1.2 → master |
comment:7 by , 12 years ago
Status: | reopened → new |
---|
comment:8 by , 9 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
That test case is fixed by f59fd15c4928caf3dfcbd50f6ab47be409a43b01 (Django 1.8). I will commit the test.
The patch applies to trunk r14233.