{{{#!python
def get_search_results(self, request, queryset, search_term):
queryset, use_distinct = super(PersonAdmin,
self).get_search_results(request, queryset, search_term)
try:
search_term_as_int = int(search_term)
queryset |= self.model.objects.filter(age=search_term_as_int)
except:
pass
return queryset, use_distinct
}}}
This code catches a `SystemExit` when all it meant to do was deal with the
case when `search_term` is not an integer. See
https://docs.python.org/2/howto/doanddont.html#except
Better would be:
{{{#!python
def get_search_results(self, request, queryset, search_term):
queryset, use_distinct = super(PersonAdmin,
self).get_search_results(request, queryset, search_term)
try:
search_term_as_int = int(search_term)
except ValueError:
pass
queryset |= self.model.objects.filter(age=search_term_as_int)
return queryset, use_distinct
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23123>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Having the `queryset |= ...` line after the except clause, as in the
suggested example, causes an exception in case `int(search_term)` fails.
--
Ticket URL: <https://code.djangoproject.com/ticket/23123#comment:1>
* stage: Unreviewed => Accepted
Comment:
How about putting `queryset != ...` in the else clause of the try/except.
--
Ticket URL: <https://code.djangoproject.com/ticket/23123#comment:2>
* owner: nobody => gmunumel
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/23123#comment:3>
* owner: gmunumel =>
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/23123#comment:4>
* owner: => Baptiste Mispelon <bmispelon@…>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"e5619330e2d3bf901155e98ef3fa7d224b6a260a"]:
{{{
#!CommitTicketReference repository=""
revision="e5619330e2d3bf901155e98ef3fa7d224b6a260a"
Fixed #23123 -- Don't use a bare except in ModelAdmin documentation
Thanks to wkschwartz for the report and to Tim for the patch.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23123#comment:5>
Comment (by Baptiste Mispelon <bmispelon@…>):
In [changeset:"9fb1652ac33f3ea7ba2f05e089d73691c9d41b8e"]:
{{{
#!CommitTicketReference repository=""
revision="9fb1652ac33f3ea7ba2f05e089d73691c9d41b8e"
[1.7.x] Fixed #23123 -- Don't use a bare except in ModelAdmin
documentation
Thanks to wkschwartz for the report and to Tim for the patch.
Backport of e5619330e2d3bf901155e98ef3fa7d224b6a260a from master.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23123#comment:6>
Comment (by Tim Graham <timograham@…>):
In [changeset:"8e25b696ba896f7f9b07565e7559aa08e743288a"]:
{{{
#!CommitTicketReference repository=""
revision="8e25b696ba896f7f9b07565e7559aa08e743288a"
[1.6.x] Fixed #23123 -- Don't use a bare except in ModelAdmin
documentation
Thanks to wkschwartz for the report and to Tim for the patch.
Backport of e5619330e2 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23123#comment:7>