Opened 8 months ago

Last modified 6 weeks ago

#28404 assigned Bug

Django admin empty_value/empty_value_display doesn't check for empty strings

Reported by: Mark Koh Owned by: Nazarov Georgiy
Component: contrib.admin Version: master
Severity: Normal Keywords: empty value display admin charfield
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: yes


According to the Django documentation, empty_value_display should "display value for record’s fields that are empty (None, empty string, etc.)" However, currently this value is only returned when the field value is None and does not check for empty strings. This can be seen here -

Thus, if you have a CharField with a value of '' (empty string), the empty_value_display value will not appear, contrary to the documentation.

How to reproduce:

  • Create a model with a CharField(blank=True, null=True)
  • Create a model_admin and add that field to the list_display list
  • Create two instances of the model, one with that field set to None and another with it set to '' (empty string)
  • Open the admin panel and find those models, you will see that the instance with None shows - but the one with the empty string shows nothing.

Attachments (1)

empty_string bug.png (66.2 KB) - added by Mark Koh 8 months ago.
Empty strings with no empty_value

Download all attachments as: .zip

Change History (12)

Changed 8 months ago by Mark Koh

Attachment: empty_string bug.png added

Empty strings with no empty_value

comment:1 Changed 8 months ago by Tim Graham

Easy pickings: unset

I'm not immediately convinced that the behavior should be changed instead of correcting the documentation to reflect the current behavior. Has the behavior changed since the documentation was introduced in 0207bdd2d4157c542c981264c86706b78ca246e9 or was the original documentation inaccurate?

comment:2 Changed 8 months ago by Mark Koh

The functionality hasn't changed since the documentation was updated, so it seems as though the original documentation was inaccurate. While I do have a PR in for the fix (WIP), I agree that it may make more sense to simply correct the documentation.
On the other hand, I do feel that this functionality would be useful as an additional overridable property for the ModelAdmin. Perhaps as an optional empty_string_display property?

comment:3 Changed 8 months ago by Tim Graham

I've grown wearisome of all the ModelAdmin hooks and complexity that they add. That's the only grounds on which I'd object to an additional property. If you have a compelling use case, I guess it's okay. I guess you're finding empty_value_display useful?

comment:4 Changed 8 months ago by Mark Koh

I would definitely find it useful if it worked with empty strings as well as None. Currently we have a field in our ModelAdmin that is a CharField with blank=True and null=False and that field is also specified in the list_display_links. Thus, when that field value is an empty string we are unable to click it (you can see this in the image attached to this ticket).

Seeing as the blank=True, null=False method is the recommended method for supporting nullable CharFields I imagine that I'm not the only person whose seen this problem. I am open to either allowing empty_value_display to acknowledge empty strings or to creating a new optional attribute for empty strings.

comment:5 Changed 8 months ago by Tim Graham

Triage Stage: UnreviewedAccepted

I don't know if adding a separate property will be simpler than modifying the existing empty_value_display. I guess there could be use cases for a separate value for each case. You could write to the DevelopersMailingList for further feedback.

comment:6 Changed 8 months ago by Tim Graham

Patch needs improvement: set

comment:7 Changed 5 months ago by Nazarov Georgiy

Owner: changed from nobody to Nazarov Georgiy
Status: newassigned

comment:8 Changed 4 months ago by Nazarov Georgiy

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin
Version: 1.11master

comment:9 Changed 4 months ago by shangdahao

Triage Stage: Ready for checkinAccepted

Hi, Nazarov Georgiy, you can't check "Ready for checkin" yourself, please see triaging-tickets

comment:10 Changed 6 weeks ago by Carlton Gibson

Patch needs improvement: set

comment:11 Changed 6 weeks ago by Carlton Gibson

I haven't yet seen a reason not to re-use empty_value_display here (rather than add a new admin option) but we need to consider when that is applied:

There are some cases where I really do want an empty string displayed (though not in a linked column). Maybe only use it only when it's an auto-linked field/column? Or maybe only use this behavior in display_for_field()? (So if you use a custom function/method, you can still have full control.) — Collin Anderson on Django Dev

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