#25609: Add a more useful error message for invalid nested lookup on a related
field
----------------------------------------------+--------------------
     Reporter:  Ian-Foote                     |      Owner:  nobody
         Type:  Cleanup/optimization          |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  master
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  1
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 If I have two models:

 {{{#!python
 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:

 {{{#!python
 Article.objects.filter(author__editor__name='James')
 }}}

 I get the unhelpful:

 {{{#!python
     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:

 {{{#!python
 TypeError: Related Field got invalid lookup: editor
 }}}

 Which matches that caused by:

 {{{#!python
 Article.objects.filter(author__editor='James')
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25609>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.8625cb5d7650cffc7305a843ffecae76%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to