#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.
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/050.88dd67d86c821add266c3b6f7e9fa591%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.