#30149: "Empty value selected check in Admin Filter prevents subclassing"
-----------------------------------------------+------------------------
               Reporter:  Sardorbek Imomaliev  |          Owner:  nobody
                   Type:  Uncategorized        |         Status:  new
              Component:  contrib.admin        |        Version:  2.1
               Severity:  Normal               |       Keywords:  admin
           Triage Stage:  Unreviewed           |      Has patch:  0
    Needs documentation:  0                    |    Needs tests:  0
Patch needs improvement:  0                    |  Easy pickings:  0
                  UI/UX:  0                    |
-----------------------------------------------+------------------------
 as per https://code.djangoproject.com/ticket/28687?cnum_edit=10#comment:11
 creating new issue, will get to writing test and PR next week

 Currently to add `Not Empty` choice you need to do this

 {{{
 #!div style="font-size: 80%"
 Code highlighting:
   {{{#!python
   class NotEmptyFilter(admin.RelatedFieldListFilter):
       @property
       def include_empty_choice(self):
           # FIXME: empty value selected incorrectly override in choices
           return False

       def has_output(self):
           return super().has_output() + 2

       def choices(self, changelist):
           yield from super().choices(changelist)
           yield {
               'selected': self.lookup_val_isnull == 'True',
               'query_string': changelist.get_query_string(
                   {self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]
               ),
               'display': _('Empty'),
           yield {
               'selected': self.lookup_val_isnull == 'False',
               'query_string': changelist.get_query_string(
                   {self.lookup_kwarg_isnull: 'False'}, [self.lookup_kwarg]
               ),
               'display': _('Not Empty'),
           }
   }}}
 }}}

 But you should be able to do just
 {{{
 #!div style="font-size: 80%"
 Code highlighting:
   {{{#!python
   class NotEmptyFilter(admin.RelatedFieldListFilter):
       def has_output(self):
           return super().has_output() + 1

       def choices(self, changelist):
           yield from super().choices(changelist)
           yield {
               'selected': self.lookup_val_isnull == 'False',
               'query_string': changelist.get_query_string(
                   {self.lookup_kwarg_isnull: 'False'}, [self.lookup_kwarg]
               ),
               'display': _('Not Empty'),
           }
   }}}
 }}}

 Currently this is not possible because of `bool(self.lookup_val_isnull)`
 check in selected for `Empty` choice, and if we add `Not Empty` choice
 like in second example we will get both `Empty` and `Not Empty` choices
 rendered as selected on `Not Empty`

 This is easy to fix, and I would like to provide PR if this change is OK

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30149>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.4624a07f1e0b3996d8290e2b5571877b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to