#22992 closed Bug (fixed)
"'GenericForeignKey' object has no attribute 'get_lookup'" exception on 1.7
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | contrib.contenttypes | Version: | 1.7-rc-1 |
| Severity: | Release blocker | Keywords: | contenttypes GenericForeignKey get_lookup |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Current 1.7 breaks querying by content_object on a model with generic relations.
>>> MonitorData.objects.get(content_object=site)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 345, in get
clone = self.filter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 689, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 707, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1287, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1314, in _add_q
current_negated=current_negated, connector=connector)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1186, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1092, in build_lookup
final_lookup = lhs.get_lookup(lookup)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/datastructures.py", line 25, in get_lookup
return self.output_field.get_lookup(name)
AttributeError: 'GenericForeignKey' object has no attribute 'get_lookup'
MonitorData looks like:
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
class MonitorData(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
value = models.PositiveIntegerField()
content_object = GenericForeignKey()
Attachments (1)
Change History (8)
comment:2 by , 11 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
Bisected to 20bab2cf9d02a5c6477d8aac066a635986e0d3f3.
comment:3 by , 11 years ago
Yes, you're right this is not a bug.
Before reporting I skimmed the docs and found the "guido" example, but not reading carefully enough to realize it was actually saying it wasn't possible!
Sorry for wasting your time Timo !
by , 11 years ago
| Attachment: | 22992-test.diff added |
|---|
comment:6 by , 11 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
From the docs (right above this section):
When I try your example, I get
FieldError: Cannot resolve keyword 'content_object' into field. Choices are: content_type, id, object_id, value. Am I missing something? Did your test work in older versions? I got the same result on 1.6.Edit: Actually, I get
FieldErroron 1.6, butAttributeError: 'GenericForeignKey' object has no attribute 'get_lookup'as you described on 1.7. Will bisect...