For example, you can test it with a query string like this:
{{{
/admin/auth/user/?username__in=johnny,viola,gordon
}}}
Unfortunately, there is a big limitation at the moment: you can't include
a value option that contains a comma (or a few).
The function that splits the string is '''prepare_lookup_value''', found
in
[https://github.com/django/django/blob/master/django/contrib/admin/util.py
contrib.admin.util].
--
Ticket URL: <https://code.djangoproject.com/ticket/19721>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Right now, the following workaround (besides monkey patching
prepare_lookup_value) works for me:
{{{#!python
def multiple_value_options_filter_factory(field_name):
"""
This is a class factory. It creates classes used
for filtering by multiple value options.
The field options are separated by a "|" character.
If any of the specified options (or "") is the
value of the field of a record, then such record
is considered matching.
The name of the field that should be using for
filtering is passed as the argument to the function.
"""
class MultipleValueOptionsFilter(admin.ListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = field_name
# Parameter for the filter that will be used in the URL query.
parameter_name = "_".join([field_name, "in"])
def __init__(self, request, params, model, model_admin):
self.used_parameters = {}
for p in self.expected_parameters():
if p in params:
value = params.pop(p)
self.used_parameters[p] = value.split("|")
def expected_parameters(self):
return [self.parameter_name]
def has_output(self):
return True
def choices(self, cl):
yield {
'selected': False,
'query_string': cl.get_query_string({},
[self.parameter_name]),
'display': 'All',
}
def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
value_options = self.used_parameters.get(self.parameter_name,
None)
if not value_options:
return queryset
filter_dict = {"__".join([field_name, "in"]): value_options}
return queryset.filter(**filter_dict)
return MultipleValueOptionsFilter
}}}
I put it in list_filter of the ModelAdmin class:
{{{#!python
list_filter = (
multiple_value_options_filter_factory("some_model_field_to_filter"),
multiple_value_options_filter_factory("some_other_model_field"),
)
}}}
And then including value options that contain commas becomes possible:
{{{
?some_model_field_to_filter_in=Look at this, it works now|Yeah, definitely
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:1>
* has_patch: 0 => 1
Comment:
Not sure if it's a good idea... I've attached a patch which allows you to
escape comma and backslash.
It will break any existing code searching for multiple backslashes (most
likely not an issue).
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:2>
* stage: Unreviewed => Accepted
Comment:
Not sure if the backslash-escape is a good idea, or if we just need to
provide a way to easily subclass the standard ListFilter and replace just
the separator character (and then document that). Either way, it should be
easier to filter on values including a comma.
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:3>
* needs_better_patch: 0 => 1
Comment:
Patch no longer applies cleanly.
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:5>
Comment (by SmileyChris):
An interesting way to solve this would be to use `getlist()` when pulling
the filter arguments without an explicit filter lookup and if a list is
found, use `__in` rather than `__exact`.
So to match the example given in the description, you'd be able to do:
{{{/admin/auth/user/?username=johnny&username=viola,with,comma}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:6>
* owner: nobody => Shreya Bamne
* status: new => assigned
Comment:
Since this is a older ticket, I tried writing a small test in
`tests\admin_filters\tests.py` to confirm the issue and I was able to do
so. I have started working on this ticket and assigning the ticket to me.
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:7>
* needs_better_patch: 1 => 0
Comment:
[https://github.com/django/django/pull/15031 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:8>
Comment (by Shreya Bamne):
I am not sure if this is the right place to ask this, but I was wondering
how long should I wait for my [https://github.com/django/django/pull/15031
PR] to get reviewed? I am new to the Django community, any help is
appreciated. Thank you.
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:9>
Comment (by Mariusz Felisiak):
Replying to [comment:9 Shreya Bamne]:
> I am not sure if this is the right place to ask this, but I was
wondering how long should I wait for my
[https://github.com/django/django/pull/15031 PR] to get reviewed? I am new
to the Django community, any help is appreciated. Thank you.
Unfortunately, you have to be patient. We try our best, but usually you
have to wait a few weeks. Thanks for preparing a patch! See also
[https://docs.djangoproject.com/en/3.2/faq/contributing/#faq-contributing-
code FAQ: Contributing code].
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:10>
Comment (by Shreya Bamne):
Thank you Mariusz! I'll keep checking the PR every week.
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:11>
* needs_better_patch: 0 => 1
* type: Bug => New feature
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:12>
Comment (by Carlton Gibson <carlton@…>):
In [changeset:"e53aea2e23b592c4774f0e9fa7f9f9d726a40dfc" e53aea2e]:
{{{
#!CommitTicketReference repository=""
revision="e53aea2e23b592c4774f0e9fa7f9f9d726a40dfc"
Refs #19721 -- Moved ModelAdmin.list_filter docs into a separate file.
Co-authored-by: Carlton Gibson <carlton...@noumenal.es>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:13>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
* needs_docs: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:14>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"8a4e5067605e608c3fcbb5ca11e0019eac8b40aa" 8a4e5067]:
{{{
#!CommitTicketReference repository=""
revision="8a4e5067605e608c3fcbb5ca11e0019eac8b40aa"
Fixed #19721 -- Allowed admin filters to customize the list separator.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:16>
Comment (by Carlton Gibson <carlton.gibson@…>):
In [changeset:"2b76f457494414f96d3848a5591909cbb48239e9" 2b76f457]:
{{{
#!CommitTicketReference repository=""
revision="2b76f457494414f96d3848a5591909cbb48239e9"
Refs #19721 -- Corrected list formatting in admin filters docs.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:15>