#25609 closed Cleanup/optimization (fixed)
Add a more useful error message for invalid nested lookup on a related field
Reported by: | Ian Foote | Owned by: | Ian Foote |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.9b1 |
Severity: | Normal | Keywords: | |
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
If I have two models:
class Author(models.Model): name = models.CharField(max_length=255) class Article(models.Model): author = models.ForeignKey(Author)
and I make the following invalid query:
Article.objects.filter(author__editor__name='James')
I get the unhelpful:
Article.objects.filter(author__editor__name='James') File "django/db/models/manager.py", line 125, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "django/db/models/query.py", line 783, in filter return self._filter_or_exclude(False, *args, **kwargs) File "django/db/models/query.py", line 801, in _filter_or_exclude clone.query.add_q(Q(*args, **kwargs)) File "django/db/models/sql/query.py", line 1239, in add_q clause, _ = self._add_q(q_object, self.used_aliases) File "django/db/models/sql/query.py", line 1265, in _add_q allow_joins=allow_joins, split_subq=split_subq, File "django/db/models/sql/query.py", line 1189, in build_filter assert len(lookups) == 1 AssertionError
I propose making the error instead:
TypeError: Related Field got invalid lookup: editor
Which matches that caused by:
Article.objects.filter(author__editor='James')
Change History (6)
comment:1 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 9 years ago
comment:3 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|---|
Version: | master → 1.9b1 |
Note:
See TracTickets
for help on using tickets.
Pull request: https://github.com/django/django/pull/5480