--
Ticket URL: <https://code.djangoproject.com/ticket/16311>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* 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>
* 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>
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>
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>
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>
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>
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>
* version: 1.3 => 1.5
--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:8>
* 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>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
Comment:
The PR lacks documentation.
--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:10>
* 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>
* 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>
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>
Comment (by stanislas.guerra@…):
That is great, many thanks Tim!
--
Ticket URL: <https://code.djangoproject.com/ticket/16311#comment:14>