Code

Opened 3 years ago

Closed 3 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: UI/UX:

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

Attachments (0)

Change History (2)

comment:1 Changed 3 years ago by russellm

  • Component changed from Uncategorized to django.contrib.admin
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 Changed 3 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

In [15817]:

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.