[Django] #16311: Option to limit list_filter to related_objects of instances of the list

17 views
Skip to first unread message

Django

unread,
Jun 21, 2011, 9:15:49 AM6/21/11
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------------------+-------------------------
Reporter: Stanislas Guerra <stan@…> | Owner: nobody
Type: New feature | Status: new
Milestone: | Component:
Version: 1.3 | contrib.admin
Keywords: admin, list_filter, | Severity: Normal
limit_choices_to | Triage Stage:
Has patch: 1 | Unreviewed
UI/UX: 0 | Easy pickings: 0
-------------------------------------------------+-------------------------
I know there are many snippets and blog entries to do that but it is a
common use case to limit the FK/related model choices to the very objects
displayed in the list and maybe the core team would consider to implement
it directly in the Django admin.

The +10 lines patch I provide allow you to limit the choices for a FK/M2M
to the current object list by adding the attribute
`RELATED_FIELD_filter_related_only=True` to your ModelAdmin.

By example :


{{{
class Order(models.Model):
...
support = models.ForeignKey(Support)
parutions = models.ManyToManyField(Parution, through='OrderParution',
related_name='order')


class OrderAdmin(admin.ModelAdmin):
...
list_filter = ('support', 'agency', 'parutions')
parutions_filter_related_only=True
support_filter_related_only=True


}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/16311>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 21, 2011, 9:52:13 AM6/21/11
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas | Owner: nobody
Guerra <stan@…> | Status: new
Type: New | Component: contrib.admin
feature | Severity: Normal
Milestone: | Keywords: admin, list_filter,
Version: 1.3 | limit_choices_to
Resolution: | Has patch: 1
Triage Stage: | Needs tests: 0
Unreviewed | Easy pickings: 0
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
Changes (by Matt@…):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Bad choice using a dict as a default argument. If you add something to
this, it will be changed in all calls to the method. Use None instead.

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:1>

Django

unread,
Jun 21, 2011, 8:17:24 PM6/21/11
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas | Owner: nobody
Guerra <stan@…> | Status: new
Type: New | Component: contrib.admin
feature | Severity: Normal
Milestone: | Keywords: admin, list_filter,
Version: 1.3 | limit_choices_to
Resolution: | Has patch: 1
Triage Stage: Accepted | Needs tests: 1
Needs documentation: 1 | Easy pickings: 0
Patch needs improvement: 1 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
Changes (by russellm):

* needs_better_patch: 0 => 1
* needs_docs: 0 => 1
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted


Comment:

Procedurally -- the dict argument is important feedback. The patch also
requires tests and documentation.

I'm inclined to say that this shouldn't be a top-level ModelAdmin
argument, but a configuration option of the list_filter item itself. The
new ListFilter class (in trunk) should provide a better interface to
handle this.

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:2>

Django

unread,
Jun 21, 2011, 8:28:37 PM6/21/11
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas | Owner: nobody
Guerra <stan@…> | Status: new
Type: New | Component: contrib.admin
feature | Severity: Normal
Milestone: | Keywords: admin, list_filter,
Version: 1.3 | limit_choices_to
Resolution: | Has patch: 1
Triage Stage: Accepted | Needs tests: 1
Needs documentation: 1 | Easy pickings: 0
Patch needs improvement: 1 |
UI/UX: 0 |
-------------------------------------+-------------------------------------

Comment (by schinckel):

Yeah, sorry that was a bit terse. Written in bed from iPad, wife
complained about tap-tap-tap.

What it should say is:

When you have a literal dict or list as a default argument to a function
or method, this generally results in undesired behaviour. If you modify
this variable, rather than assign to it, then you change the default value
for _every_ call to the function/method.

ie (this is a list, but a dict is the same behaviour):


{{{
def foo(bar=[]):
return bar

baz = foo()
baz # =>[]
baz.append(None)
baz # => [None]
foo() # => [None]
}}}

You almost always expect that last call to ''foo()'' to return ''[]'', not
''[None]''.

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:3>

Django

unread,
Jun 21, 2011, 8:43:32 PM6/21/11
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas | Owner: nobody
Guerra <stan@…> | Status: new
Type: New | Component: contrib.admin
feature | Severity: Normal
Milestone: | Keywords: admin, list_filter,
Version: 1.3 | limit_choices_to
Resolution: | Has patch: 1
Triage Stage: Accepted | Needs tests: 1
Needs documentation: 1 | Easy pickings: 0
Patch needs improvement: 1 |
UI/UX: 0 |
-------------------------------------+-------------------------------------

Comment (by julien):

I agree with russellm for the API design; the class you specifically want
to look at is `contrib.admin.filters.RelatedFieldListFilter`. The
functionality of that class could be extended to accept configuration
options. The developer could then assign a custom filter class to a given
field:

{{{#!python
class MyModelAdmin(ModelAdmin):
list_filter = (('my_field', MyRelatedFieldListFilter),)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:4>

Django

unread,
Jun 22, 2011, 5:36:15 AM6/22/11
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas | Owner: nobody
Guerra <stan@…> | Status: new
Type: New | Component: contrib.admin
feature | Severity: Normal
Milestone: | Keywords: admin, list_filter,
Version: 1.3 | limit_choices_to
Resolution: | Has patch: 1
Triage Stage: Accepted | Needs tests: 1
Needs documentation: 1 | Easy pickings: 0
Patch needs improvement: 1 |
UI/UX: 0 |
-------------------------------------+-------------------------------------

Comment (by Stanislas Guerra <stan@…>):

Thanks for the feedback.

I have rewritten the patch for the trunk according to your suggestions and
the usage is now :


{{{
from django.contrib.admin.filters import RelatedOnlyFieldListFilter

class OrderAdmin(admin.ModelAdmin):
...
list_filter = ('support', 'agency', ('parutions',
RelatedOnlyFieldListFilter))

}}}

Writting some tests now.

By the way, is `list_filer` a typo in `contrib.admin.views.main` (line 82)
?


{{{
for list_filer in self.list_filter:
if callable(list_filer):
# This is simply a custom list filter class.
spec = list_filer(request, cleaned_params,
self.model, self.model_admin)
else:
field_path = None
if isinstance(list_filer, (tuple, list)):
# This is a custom FieldListFilter class for a
given field.
field, field_list_filter_class = list_filer
else:
# This is simply a field name, so use the default
# FieldListFilter class that has been registered
for
# the type of the given field.
field, field_list_filter_class = list_filer,
FieldListFilter.create

}}}

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

Django

unread,
Jun 22, 2011, 8:20:54 AM6/22/11
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas | Owner: nobody
Guerra <stan@…> | Status: new
Type: New | Component: contrib.admin
feature | Severity: Normal
Milestone: | Keywords: admin, list_filter,
Version: 1.3 | limit_choices_to
Resolution: | Has patch: 1
Triage Stage: Accepted | Needs tests: 1
Needs documentation: 1 | Easy pickings: 0
Patch needs improvement: 1 |
UI/UX: 0 |
-------------------------------------+-------------------------------------

Comment (by jezdez):

In [16445]:
{{{
#!CommitTicketReference repository="" revision="16445"
Fixed typos in admin views wrt list_filter. Refs #16311.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:6>

Django

unread,
Sep 5, 2013, 3:40:23 AM9/5/13
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas Guerra | Owner: nobody
<stan@…> | Status: new
Type: New feature | Version: 1.3
Component: contrib.admin | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: admin, list_filter, | Needs documentation: 1
limit_choices_to | Patch needs improvement: 1
Has patch: 1 | UI/UX: 0
Needs tests: 1 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by stanislas.guerra@…):

Patch against 1.5.x :
https://github.com/Starou/django/compare/django:stable/1.5.x...ticket_16311_1_5?expand=1

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:7>

Django

unread,
Sep 5, 2013, 3:40:41 AM9/5/13
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas Guerra | Owner: nobody
<stan@…> | Status: new
Type: New feature | Version: 1.5

Component: contrib.admin | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: admin, list_filter, | Needs documentation: 1
limit_choices_to | Patch needs improvement: 1
Has patch: 1 | UI/UX: 0
Needs tests: 1 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by stanislas.guerra@…):

* version: 1.3 => 1.5


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

Django

unread,
May 21, 2014, 6:42:43 AM5/21/14
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas Guerra | Owner: nobody
<stan@…> | Status: new
Type: New feature | Version: master

Component: contrib.admin | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: admin, list_filter, | Needs documentation: 1
limit_choices_to | Patch needs improvement: 1
Has patch: 1 | UI/UX: 0
Needs tests: 1 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by stanislas.guerra@…):

* version: 1.5 => master


Comment:

PR against master : https://github.com/django/django/pull/2695

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:9>

Django

unread,
Jul 8, 2014, 12:48:02 PM7/8/14
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas Guerra | Owner: nobody
<stan@…> | Status: new
Type: New feature | Version: master
Component: contrib.admin | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: admin, list_filter, | Needs documentation: 1
limit_choices_to | Patch needs improvement: 0

Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: 1 => 0
* needs_tests: 1 => 0


Comment:

The PR lacks documentation.

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:10>

Django

unread,
Jul 9, 2014, 4:53:15 AM7/9/14
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas Guerra | Owner: nobody
<stan@…> | Status: new
Type: New feature | Version: master
Component: contrib.admin | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: admin, list_filter, | Needs documentation: 0

limit_choices_to | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by stanislas.guerra@…):

* needs_docs: 1 => 0


Comment:

Replying to [comment:10 timo]:
> The PR lacks documentation.

Hi Timo,

PR updated.

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:11>

Django

unread,
Aug 4, 2014, 9:39:57 AM8/4/14
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas Guerra | Owner: nobody
<stan@…> | Status: closed

Type: New feature | Version: master
Component: contrib.admin | Resolution: fixed

Severity: Normal | Triage Stage: Accepted
Keywords: admin, list_filter, | Needs documentation: 0
limit_choices_to | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"98e8da3709e400d549e87c7ff1450a2685c0adcf"]:
{{{
#!CommitTicketReference repository=""
revision="98e8da3709e400d549e87c7ff1450a2685c0adcf"
Fixed #16311 -- Added a RelatedOnlyFieldListFilter class in admin.filters.
}}}

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

Django

unread,
Aug 4, 2014, 10:22:09 AM8/4/14
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas Guerra | Owner: nobody
<stan@…> | Status: closed
Type: New feature | Version: master
Component: contrib.admin | Resolution: fixed
Severity: Normal | Triage Stage: Accepted
Keywords: admin, list_filter, | Needs documentation: 0
limit_choices_to | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"b50ea73e1e0179c1925167832318a6d0316ee50b"]:
{{{
#!CommitTicketReference repository=""
revision="b50ea73e1e0179c1925167832318a6d0316ee50b"
Fixed two tests in previous commit; refs #16311.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:13>

Django

unread,
Aug 6, 2014, 4:07:34 AM8/6/14
to django-...@googlegroups.com
#16311: Option to limit list_filter to related_objects of instances of the list
-------------------------------------+-------------------------------------
Reporter: Stanislas Guerra | Owner: nobody
<stan@…> | Status: closed
Type: New feature | Version: master
Component: contrib.admin | Resolution: fixed
Severity: Normal | Triage Stage: Accepted
Keywords: admin, list_filter, | Needs documentation: 0
limit_choices_to | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by stanislas.guerra@…):

That is great, many thanks Tim!

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

Reply all
Reply to author
Forward
0 new messages