#28404: Django admin empty_value/empty_value_display doesn't check for empty
strings
-------------------------------------+-------------------------------------
     Reporter:  Mark Koh             |                    Owner:  Nazarov
                                     |  Georgiy
         Type:  Bug                  |                   Status:  assigned
    Component:  contrib.admin        |                  Version:  dev
     Severity:  Normal               |               Resolution:
     Keywords:  empty value display  |             Triage Stage:  Accepted
  admin charfield                    |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  1
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 Given the `db.models.Field` has
 
[https://github.com/django/django/blob/62ffc9883afdc0a9f9674702661062508230d7bf/django/db/models/fields/__init__.py#L120
 an API that defines what empty values should be] I think that admin logic
 should be changed to rely on it?

 {{{#!python
 diff --git a/django/contrib/admin/templatetags/admin_list.py
 b/django/contrib/admin/templatetags/admin_list.py
 index 5865843dce..a570026b1e 100644
 --- a/django/contrib/admin/templatetags/admin_list.py
 +++ b/django/contrib/admin/templatetags/admin_list.py
 @@ -227,7 +227,7 @@ def link_in_col(is_first, field_name, cl):
              else:
                  if isinstance(f.remote_field, models.ManyToOneRel):
                      field_val = getattr(result, f.name)
 -                    if field_val is None:
 +                    if field_val in f.empty_values:
                          result_repr = empty_value_display
                      else:
                          result_repr = field_va
 }}}

 That would at least make the notion of ''empty'' coherent between models
 and the admin. It kind of blurs the line it terms of what change list
 filtering by empty value mean when there's multiple candidates though
 (e.g. `CharField(blank=True, null=True)`). Kind of wish we had an
 `__empty` lookup for this purpose that would be an alias for all allowed
 empty values by the field definition:

 {{{#!python
 class EmptyModel(models.Model):
     text = TextField(blank=True, null=False)
     nullable_text = TextField(blank=True, null=True)
     integer = IntegerField(blank=True, null=True)
     array = ArrayField(TextField(), null=True)
     json = JSONField()

 EmptyModel.objects.filter(
     text__empty=True,  # Q(text="")
     nullable_text__empty=True,  # Q(nullable_text="") |
 Q(nullable_text=None)
     integer__empty=True,  # Q(integer=None)
     array__empty=True,  # Q(array=[]) | Q(array=None)
     json__empty=True,  # Q(json=[]) | Q(json={}) | Q(json=None)
 )
 }}}

 If this existed the admin filter could for empty value could basically be
 `__empty=True`
 
[https://github.com/django/django/blob/7119f40c9881666b6f9b5cf7df09ee1d21cc8344/django/contrib/admin/filters.py#L247
 instead of] `__isnull=True`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28404#comment:15>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018024436c85-97b5e86f-2bd5-4e28-8d0a-b3c801e5096a-000000%40eu-central-1.amazonses.com.

Reply via email to