-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
list_filter behaviour yields empty lists #116
Comments
Hi! @ZippoLag, any luck with the filter? |
Workaround:class FieldNameFilter(admin.ChoicesFieldListFilter):
title = 'field_name'
parameter_name = 'field_name'
def __init__(self, field, request, params, model, model_admin, field_path):
super().__init__(field, request, params, model, model_admin, field_path)
self.lookup_kwarg = '%s__icontains' % field_path |
Thanks @FedeG for providing this workaround. For the "selected" option to work, I had to override both lookup_kwarg and lookup_val (see below code). class FieldNameFilter(admin.ChoicesFieldListFilter):
title = 'field_name'
parameter_name = 'field_name'
def __init__(self, field, request, params, model, model_admin, field_path):
super().__init__(field, request, params, model, model_admin, field_path)
self.lookup_kwarg = '%s__icontains' % field_path
self.lookup_val = params.get(self.lookup_kwarg) |
Workaround2 Based on the docs multiselect storage a charfield with the options selected separated by commas
|
Thanks for the above! Using that I've hacked something together which can be used with def _multiple_choice_filter(field_name):
class FieldNameFilter(admin.ChoicesFieldListFilter):
title = field_name
parameter_name = field_name
def __init__(self, field, request, params, model, model_admin, field_path):
super().__init__(field, request, params, model, model_admin, field_path)
self.lookup_kwarg = "%s__icontains" % field_path
self.lookup_val = params.get(self.lookup_kwarg)
return (field_name, FieldNameFilter)
class ObjectAdmin(admin.ModelAdmin):
list_filter = [_multiple_choice_filter("field_name")] |
If we include a
MultiSelectField
in a model's Adminlist_filter
then when we click on any of the "filter" links in the list page we get redirect to a URL that uses?field_name__exact=choice
and hence returns no valid results.However, tampering with the URL and changing it to
?field_name__icontains=choice
works as expected.I've poked around and I believe that we could create our own filter derived from
admin.ChoicesFieldListFilter
(defined infilters.py
), but I'm not sure even where to begin with.The text was updated successfully, but these errors were encountered: