[Django] #19721: Django admin allows filtering using the field lookups such as "in", but it is impossible to include a value that contains a comma

14 views
Skip to first unread message

Django

unread,
Feb 1, 2013, 4:39:19 PM2/1/13
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+--------------------
Reporter: aruseni | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
The admin site allows you to filter the queryset in the changelist in a
plenty of different ways. Notably, it allows you to filter the records by
multiple values (if the field's value is one of the specified value
options, then such record is considered matching).

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.

Django

unread,
Feb 1, 2013, 8:21:37 PM2/1/13
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+--------------------------------------

Reporter: aruseni | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by aruseni):

* 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>

Django

unread,
Feb 9, 2013, 8:15:47 PM2/9/13
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+--------------------------------------

Reporter: aruseni | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by anonymous):

* 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>

Django

unread,
Feb 22, 2013, 12:11:13 PM2/22/13
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+------------------------------------

Reporter: aruseni | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by carljm):

* 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>

Django

unread,
Jun 25, 2014, 7:30:59 AM6/25/14
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+------------------------------------
Reporter: aruseni | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by timo):

* needs_better_patch: 0 => 1


Comment:

Patch no longer applies cleanly.

--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:5>

Django

unread,
Aug 3, 2014, 10:05:24 PM8/3/14
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+------------------------------------
Reporter: aruseni | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

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>

Django

unread,
Oct 27, 2021, 2:06:26 PM10/27/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+----------------------------------------
Reporter: aruseni | Owner: Shreya Bamne
Type: Bug | Status: assigned
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------
Changes (by Shreya Bamne):

* 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>

Django

unread,
Oct 28, 2021, 12:34:53 PM10/28/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+----------------------------------------
Reporter: aruseni | Owner: Shreya Bamne
Type: Bug | Status: assigned
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0


Comment:

[https://github.com/django/django/pull/15031 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:8>

Django

unread,
Nov 8, 2021, 8:28:19 PM11/8/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+----------------------------------------
Reporter: aruseni | Owner: Shreya Bamne
Type: Bug | Status: assigned
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------

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>

Django

unread,
Nov 9, 2021, 12:12:39 AM11/9/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+----------------------------------------
Reporter: aruseni | Owner: Shreya Bamne
Type: Bug | Status: assigned
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------

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>

Django

unread,
Nov 9, 2021, 8:51:00 AM11/9/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+----------------------------------------
Reporter: aruseni | Owner: Shreya Bamne
Type: Bug | Status: assigned
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------

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>

Django

unread,
Nov 10, 2021, 5:51:49 AM11/10/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+----------------------------------------
Reporter: aruseni | Owner: Shreya Bamne
Type: New feature | Status: assigned
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 0 => 1

* type: Bug => New feature
* needs_docs: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:12>

Django

unread,
Nov 18, 2021, 8:01:30 AM11/18/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------+----------------------------------------
Reporter: aruseni | Owner: Shreya Bamne
Type: New feature | Status: assigned
Component: contrib.admin | Version: 1.4

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------

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>

Django

unread,
Dec 8, 2021, 9:32:24 AM12/8/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------------+-------------------------------------

Reporter: aruseni | Owner: Shreya
| Bamne
Type: New feature | Status: assigned
Component: contrib.admin | Version: 1.4
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/19721#comment:14>

Django

unread,
Dec 8, 2021, 10:10:16 AM12/8/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Shreya
| Bamne
Type: New feature | Status: closed
Component: contrib.admin | Version: 1.4
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson <carlton.gibson@…>):

* 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>

Django

unread,
Dec 8, 2021, 10:10:16 AM12/8/21
to django-...@googlegroups.com
#19721: Django admin allows filtering using the field lookups such as "in", but it
is impossible to include a value that contains a comma
-------------------------------------+-------------------------------------
Reporter: aruseni | Owner: Shreya
| Bamne
Type: New feature | Status: assigned
Component: contrib.admin | Version: 1.4
Severity: Normal | Resolution:

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages