Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#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 Ian Foote, 9 years ago

Owner: changed from nobody to Ian Foote
Status: newassigned

comment:3 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin
Version: master1.9b1

comment:5 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 32ef48a:

Fixed #25609 -- Fixed regression in related field nested lookup error.

comment:6 by Tim Graham <timograham@…>, 9 years ago

In 32e804cd:

[1.9.x] Fixed #25609 -- Fixed regression in related field nested lookup error.

Backport of 32ef48aa562e6aaee9983f5d0f1c60f02fd555fb from master

Note: See TracTickets for help on using tickets.
Back to Top