#19318 closed Bug (fixed)
Admin's SimpleListFilter option not being displayed as selected if the lookup's first element is not a string
| Reported by: | Sebastián Magrí | Owned by: | nobody |
|---|---|---|---|
| Component: | contrib.admin | Version: | 1.4 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | yes | Patch needs improvement: | yes |
| Easy pickings: | yes | UI/UX: | no |
Description
I had the following lookups method for a SimpleListFilter:
def lookups(self, request, model_admin):
return set(itertools.chain(*[
[(o.deliverable.id, o.deliverable.name)
for o in user.orders.all()]
for user in model_admin.queryset(request).all()
]))
In this case, the filters in the change list didn't display the option as selected on activation.
Changing the above code to
def lookups(self, request, model_admin):
return set(itertools.chain(*[
[(str(o.deliverable.id), o.deliverable.name)
for o in user.orders.all()]
for user in model_admin.queryset(request).all()
]))
Fixed the issue.
This seems to be caused by django/contrib/admin/filters.py:105, where
yield {
'selected': self.value() == lookup,
'query_string': cl.get_query_string({
self.parameter_name: lookup,
}, []),
'display': title,
}
Might be
yield {
'selected': self.value() == str(lookup),
'query_string': cl.get_query_string({
self.parameter_name: lookup,
}, []),
'display': title,
}
I've created a pull request at GitHub with this change.
Change History (7)
comment:1 by , 13 years ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
| Triage Stage: | Unreviewed → Accepted |
| UI/UX: | unset |
comment:2 by , 13 years ago
I have updated the pull request to use force_unicode instead of str.
Regarding tests, I would like to get some help on it as I don't have too much experience with testing.
comment:3 by , 13 years ago
Take a look at the existing tests in tests/regressiontests/admin_filters/tests.py. You should find multiple examples to get started. Have a go at it and then come back with questions if you still need any further assistance.
comment:4 by , 13 years ago
I've closed the old pull requests and I have created two new pull requests, one for 1.4.x and the other for 1.5.x. The fix in different in both cases. Because of the py3k compatibility in 1.5 the force_unicode utility function have been renamed to force_text.
Pull requests are 539 and 540.
Please let me know if there is any other requirement.
Best Regards,
comment:5 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Thanks for the report. Using
force_unicodewould be more appropriate thanstr.