#3096 closed defect (fixed)
Filtering choices in admin list view don't respond to limit_choices_to parameter
Reported by: | Archatas (aidas.bendoraitis at gmail.com) | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | newforms-admin |
Severity: | normal | Keywords: | nfa-someday yandex-sprint ep2008 |
Cc: | myer0052@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Administration list view shows choices in filters not responding to the limit_choices_to field for the field.
For example, if we have
class Color(models.Model): value=models.CharField(...) warm=models.BooleanField(...) class Thing(models.Model): title=models.CharField(...) color=models.ForeignKey(Color, limit_choices_to={'warm': True}) class Admin: list_filter = ('color',)
We will get all the choices of Color objects listed in the Filter/By Color section instead of getting only those, which are warm.
To fix this, the line
self.lookup_choices = f.rel.to._default_manager.all()
at class RelatedFilterSpec(FilterSpec) in the file django/contrib/admin/filterspecs.py has to be changed to
if f.rel.limit_choices_to: self.lookup_choices = f.rel.to._default_manager.filter(**f.rel.limit_choices_to) else: self.lookup_choices = f.rel.to._default_manager.all()
Attachments (4)
Change History (20)
comment:1 by , 18 years ago
milestone: | Version 1.0 |
---|
comment:2 by , 18 years ago
Cc: | added |
---|
comment:3 by , 18 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
This seems like the logical thing to do. If someone disagrees, feel free to set back to decision needed. Can someone provide a real patch please.
comment:4 by , 18 years ago
Patch needs improvement: | unset |
---|
I tested out the patch and it works. Not sure it this should have a unit test or not it seems really simple and none of the other filters are currently being tested.
comment:5 by , 18 years ago
I found one case, where it fails: that is when Q() objects instead of dictionaries are used to limit the choices. Here is the simple fix:
if f.rel.limit_choices_to: if type(f.rel.limit_choices_to).__name__ == "dict": self.lookup_choices = f.rel.to._default_manager.filter(**f.rel.limit_choices_to) else: self.lookup_choices = f.rel.to._default_manager.filter(f.rel.limit_choices_to) else: self.lookup_choices = f.rel.to._default_manager.all()
comment:6 by , 17 years ago
The patch by Archatas doesn't work with the following limit_choices_to:
limit_choices_to = {'parentisnull': False}
by , 17 years ago
Attachment: | 3096-filterspecs-2.patch added |
---|
Take limit_choices_to
into account for list_filter
in Admin
comment:7 by , 17 years ago
The same thing is done in the get_choices()
method in /django/db/models/fields/__init__.py
, ideally we would reuse this, but because I don't want to rewrite a function I don't completely understand, I have just copied the approach taken in get_choices().
The patch I just attached should work exactly the same way as get_choices()
.
comment:8 by , 17 years ago
Patch needs improvement: | set |
---|---|
Summary: | Filtering choices in admin list view don't respond to limit_choices_to parameter → [newforms-admin] - Filtering choices in admin list view don't respond to limit_choices_to parameter |
Version: | SVN → newforms-admin |
The admin code in trunk won't be updated. Moving this to newforms-admin. It does look to still be a problem in that branch. Please submit patches against newforms-admin.
comment:9 by , 17 years ago
Summary: | [newforms-admin] - Filtering choices in admin list view don't respond to limit_choices_to parameter → Filtering choices in admin list view don't respond to limit_choices_to parameter |
---|
(Please don't change ticket titles to make them less readable by chewing up space with an unnecessary prefix. We already have the version field and patch checkboxes and the like to differentiate between things.)
by , 17 years ago
Attachment: | 3096-filterspecs-newforms-admin.diff added |
---|
newforms-admin
version of patch to take limit_choices_to
into account for list_filter
in Admin
comment:11 by , 17 years ago
Keywords: | nfa-someday added |
---|
Problem originally reported against old admin, should not block merge of newforms-admin.
comment:12 by , 17 years ago
doesn't matter as the problem still exists and is major as it may render admin pages unusable.
comment:13 by , 16 years ago
Keywords: | yandex-sprint added |
---|---|
Patch needs improvement: | unset |
As suggested by comment:7 new patch uses field's get_choices to construct a list of (id, title) pairs. Though a bit bigger (3 lines instead of 1) this looks cleaner to me.
comment:14 by , 16 years ago
Keywords: | ep2008 added |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:15 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Milestone Version 1.0 deleted