Opened 7 years ago
Closed 7 years ago
#29135 closed Cleanup/optimization (fixed)
get_object_or_404() and get_list_or_404() swallow AttributeError in queryset filtering
Reported by: | David Hagen | Owned by: | Yuri Shikanov |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
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
'.
Change History (7)
comment:1 by , 7 years ago
Component: | Uncategorized → Core (Other) |
---|---|
Summary: | `get_object_or_404` and `get_list_or_404` swallows exceptions → get_object_or_404() and get_list_or_404() swallow AttributeError in queryset filtering |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
comment:2 by , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 7 years ago
comment:4 by , 7 years ago
Has patch: | set |
---|
comment:5 by , 7 years ago
Patch needs improvement: | set |
---|
Just minor edits on patch needed. (Comments on PR.)
comment:6 by , 7 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Note:
See TracTickets
for help on using tickets.
I've opened PR