[Django] #29135: `get_object_or_404` and `get_list_or_404` swallows exceptions

12 views
Skip to first unread message

Django

unread,
Feb 15, 2018, 2:41:59 PM2/15/18
to django-...@googlegroups.com
#29135: `get_object_or_404` and `get_list_or_404` swallows exceptions
-----------------------------------------+------------------------
Reporter: David Hagen | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+------------------------
Here is the code for `get_list_or_404`:

{{{
def get_list_or_404(klass, *args, **kwargs):
queryset = _get_queryset(klass)
try:
obj_list = list(queryset.filter(*args, **kwargs))
except AttributeError:
klass__name = klass.__name__ if isinstance(klass, type) else
klass.__class__.__name__
raise ValueError(
"First argument to get_list_or_404() must be a Model, Manager,
or "
"QuerySet, not '%s'." % klass__name
)
if not obj_list:
raise Http404('No %s matches the given query.' %
queryset.model._meta.object_name)
return obj_list
}}}

The try-catch block is far too broad. Any `AttributeError` raised during
the call to `filter` is swallowed and converted into an incorrect and
confusing error message. This should be changed to either `if not
hasattr(queryset, 'filter'): raise ...` or `try: method = queryset.filter;
except AttributeError: ...`. The same thing happens with
`queryset.get(*args, **kwargs)` in `get_object_or_404`'.

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

Django

unread,
Feb 15, 2018, 6:43:30 PM2/15/18
to django-...@googlegroups.com
#29135: get_object_or_404() and get_list_or_404() swallow AttributeError in
queryset filtering
--------------------------------------+------------------------------------

Reporter: David Hagen | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
* component: Uncategorized => Core (Other)


--
Ticket URL: <https://code.djangoproject.com/ticket/29135#comment:1>

Django

unread,
Feb 16, 2018, 2:29:51 AM2/16/18
to django-...@googlegroups.com
#29135: get_object_or_404() and get_list_or_404() swallow AttributeError in
queryset filtering
-------------------------------------+-------------------------------------
Reporter: David Hagen | Owner: Yuri
Type: | Shikanov
Cleanup/optimization | Status: assigned

Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Yuri Shikanov):

* owner: nobody => Yuri Shikanov
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/29135#comment:2>

Django

unread,
Feb 17, 2018, 3:40:39 AM2/17/18
to django-...@googlegroups.com
#29135: get_object_or_404() and get_list_or_404() swallow AttributeError in
queryset filtering
-------------------------------------+-------------------------------------
Reporter: David Hagen | Owner: Yuri
Type: | Shikanov
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Yuri Shikanov):

I've opened [https://github.com/django/django/pull/9705 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29135#comment:3>

Django

unread,
Feb 17, 2018, 3:41:12 AM2/17/18
to django-...@googlegroups.com
#29135: get_object_or_404() and get_list_or_404() swallow AttributeError in
queryset filtering
-------------------------------------+-------------------------------------
Reporter: David Hagen | Owner: Yuri
Type: | Shikanov
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Yuri Shikanov):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/29135#comment:4>

Django

unread,
Feb 19, 2018, 5:58:05 AM2/19/18
to django-...@googlegroups.com
#29135: get_object_or_404() and get_list_or_404() swallow AttributeError in
queryset filtering
-------------------------------------+-------------------------------------
Reporter: David Hagen | Owner: Yuri
Type: | Shikanov
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 0 => 1


Comment:

Just minor edits on patch needed. (Comments on PR.)

--
Ticket URL: <https://code.djangoproject.com/ticket/29135#comment:5>

Django

unread,
Feb 19, 2018, 7:10:55 AM2/19/18
to django-...@googlegroups.com
#29135: get_object_or_404() and get_list_or_404() swallow AttributeError in
queryset filtering
-------------------------------------+-------------------------------------
Reporter: David Hagen | Owner: Yuri
Type: | Shikanov
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/29135#comment:6>

Django

unread,
Feb 19, 2018, 11:18:55 AM2/19/18
to django-...@googlegroups.com
#29135: get_object_or_404() and get_list_or_404() swallow AttributeError in
queryset filtering
-------------------------------------+-------------------------------------
Reporter: David Hagen | Owner: Yuri
Type: | Shikanov
Cleanup/optimization | Status: closed

Component: Core (Other) | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"eb002e7892793fb9f593efaed8d3ea52a2bfc96c" eb002e78]:
{{{
#!CommitTicketReference repository=""
revision="eb002e7892793fb9f593efaed8d3ea52a2bfc96c"
Fixed #29135 -- Prevented get_object/list_or_404() from hiding
AttributeError raised by QuerySet filtering.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29135#comment:7>

Reply all
Reply to author
Forward
0 new messages