Opened 9 years ago
Closed 9 years ago
#26277 closed New feature (fixed)
The list_display for CharField doesn't work for None value among choices
Reported by: | Petr Dlouhý | Owned by: | Vincenzo Pandolfo |
---|---|---|---|
Component: | contrib.admin | Version: | 1.9 |
Severity: | Normal | Keywords: | list_filter CharField admin |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | yes |
Description
I have a following model field:
DIRECTIONS = [ ('trip_to', "From"), ('trip_from', "To"), (None, "None"), ('', "-"), ] direction = models.CharField( verbose_name="direction", choices=DIRECTIONS, max_length=20, null=True, blank=True)
I want to filter out the items with direction set to Null in my admin list display. Both the choices (None
and ''
) are displayed in the filter, but none of them works. The None choice generates URL without the direction__exact
parameter. The ''
choice generates direction__exact=
parameter, which also does not work.
What work is, if I set the URL parameter manually to direction__isnull=True
, but the link is not generated that way.
My suggestion is, that when the CharField has null=True
and blank=True
, then there is always Unknown choice in list_filter
and if the None
or ''
is in choices, than the choice is named that way.
Change History (9)
comment:1 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 9 years ago
comment:4 by , 9 years ago
Where is the empty string to null conversation happening? I think it should be possible to store both values and hence both should appear in the admin filter choices.
comment:5 by , 9 years ago
I am not sure where this is happening but in the (limited) testing I have done it appears that when both blank and null are set to True a record created picking the empty string choice will be displayed as having the null value choice.
I will try testing with different databases to see where the issue lies, since I didn't find anything related in the Django source code.
comment:6 by , 9 years ago
Has patch: | set |
---|
comment:7 by , 9 years ago
Patch needs improvement: | set |
---|
Left a few comments for improvement on the PR.
comment:8 by , 9 years ago
Patch needs improvement: | unset |
---|
I am working on a patch here: https://github.com/vincepandolfo/django/tree/ticket_26277
I did not send a pull request yet because I am not sure if this is the behaviour we want it to have.
My current implementation allows a None value to be in the choices of a
ChoicesFieldListFilter
and, in the case of aCharField
, if the empty string is among the choices andnull=True
it ignores it since empty strings are converted to Null in the database.