Opened 4 years ago

Closed 4 years ago

#31636 closed Bug (fixed)

BooleanFieldListFilter doesn't respect field choices.

Reported by: Maxence G Owned by: Manav Agarwal
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

If I have such construction:

# models.py
class BoolTest(models.Model):
    BOUGHT_CHOICES = (
        (False, 'Pending'),
        (True, 'Bought')
    )
    bought = models.BooleanField(
        verbose_name="Fancy Boolean",
        default=False,
        choices=BOUGHT_CHOICES)

# admin.py
class BoolTestAdmin(admin.ModelAdmin):
    list_filter = ('bought',)
    
admin.site.register(BoolTest, BoolTestAdmin)

The boolean Filter text is not modified to fit choices param

Example (in FR):

FILTRE
Par Fancy Boolean

  • Tout
  • Oui
  • Non

Should be :

FILTRE
Par Fancy Boolean

  • Tout
  • Bought
  • Pending

Change History (9)

comment:1 by Mariusz Felisiak, 4 years ago

Summary: Django Bool choices text and admin filterBooleanFieldListFilter doesn't respect field choices.
Triage Stage: UnreviewedAccepted
Version: 3.0master

Thanks, we could probably use field.flatchoices, e.g.

diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py
index 3e02cd89d7..9fdf038085 100644
--- a/django/contrib/admin/filters.py
+++ b/django/contrib/admin/filters.py
@@ -244,10 +244,7 @@ class BooleanFieldListFilter(FieldListFilter):
         return [self.lookup_kwarg, self.lookup_kwarg2]
 
     def choices(self, changelist):
-        for lookup, title in (
-                (None, _('All')),
-                ('1', _('Yes')),
-                ('0', _('No'))):
+        for lookup, title in ((None, _('All')), *self.field.flatchoices):
             yield {
                 'selected': self.lookup_val == lookup and not self.lookup_val2,
                 'query_string': changelist.get_query_string({self.lookup_kwarg: lookup}, [self.lookup_kwarg2]),

comment:2 by Jithin Tom, 4 years ago

Owner: changed from nobody to Jithin Tom
Status: newassigned

comment:3 by Mariusz Felisiak, 4 years ago

Has patch: set
Needs tests: set
Patch needs improvement: set

comment:5 by Manav Agarwal, 4 years ago

May i work on this issue?

comment:6 by Manav Agarwal, 4 years ago

Owner: changed from Jithin Tom to Manav Agarwal

comment:7 by Mariusz Felisiak, 4 years ago

Needs tests: unset
Patch needs improvement: unset

comment:8 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:9 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 580a4341:

Fixed #31636 -- Made BooleanFieldListFilter respect Field.choices.

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