Opened 13 years ago

Closed 13 years ago

#15606 closed (fixed)

Django admin filter and Bool choices

Reported by: django@… Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Keywords:
Cc: mir@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I have such construction:

# models.py
class BoolTest(models.Model):
    NO = False
    YES = True
    YES_NO_CHOICES = (
        (NO, 'no'),
        (YES, 'yes')
    )
    completed = models.BooleanField(
        default=NO,
        choices=YES_NO_CHOICES

# admin.py
class BoolTestAdmin(admin.ModelAdmin):
    list_filter = ('completed',)

and I use filter results in Django admin, then this URL is generated (for no and yes choices):

.../admin/appname/booltest/?completed__exact=False
.../admin/appname/booltest/?completed__exact=True

The True/False is taken as string (not converted 'True' -> True and 'False' -> False), non empty strings in Python are considered as True, so the final SQL is for both choices:

SELECT appname_booltest.id, appname_booltest.name, appname_booltest.completed FROM appname_booltest WHERE appname_booltest.completed = 1 ORDER BY appname_booltest.id DESC

I think, that instead of True/False there should be 0/1 in URL

Change History (2)

comment:1 by Russell Keith-Magee, 13 years ago

Component: Uncategorizeddjango.contrib.admin
Triage Stage: UnreviewedAccepted

The problem here is the fact that you've used choices on a BooleanField; as a result of this, the "choices" filterspec is used, rather than the "boolean" filterspec.

This can be corrected by registering the Boolean filterspec first -- that way, the Boolean field will always get the 0/1 filter required by a boolean field.

comment:2 by Russell Keith-Magee, 13 years ago

Resolution: fixed
Status: newclosed

In [15817]:

Fixed #15606 -- Ensured that boolean fields always use the Boolean filterspec. Thanks to Martin Tiršel for the report

Note: See TracTickets for help on using tickets.
Back to Top