Issue with get_FOO_display not working in Django admin

213 views
Skip to first unread message

Ibrahim Abou Elenein

unread,
Apr 4, 2023, 7:10:25 PM4/4/23
to Django developers (Contributions to Django itself)
Dear All,

I am writing to report an issue I encountered while working with Django admin. I had a model with a field that uses Choices as follows:

```
status = FSMField(default=STATUSES.PENDING, choices=STATUSES, protected=True)
```
I overrode the get_status_display method, but to my surprise, it did not have any effect in the Django admin.

Upon investigating Django's code, I found the following method in contrib.admin

```
def display_for_field(value, field, empty_value_display):
    from django.contrib.admin.templatetags.admin_list import _boolean_icon

    if getattr(field, "flatchoices", None):
        return dict(field.flatchoices).get(value, empty_value_display)

```
I noticed that this method uses flatchoices to get the display value for fields with choices. However, it does not take into account any custom display methods that may have been defined for the field.

To resolve this issue, I modified the code to use get_FOO_display instead of flatchoices as follows:

```
def display_for_field(value, field, empty_value_display, model=None):
    from django.contrib.admin.templatetags.admin_list import _boolean_icon

    if getattr(field, "flatchoices", None):
        if model:
            return getattr(model, "get_%s_display" % field.name)()
        return dict(field.flatchoices).get(value, empty_value_display)

```
This modification allowed my custom display method to work as expected in the Django admin.

However, I am curious to know why Django's code behaves this way and how I can make use of this behavior in my application.

Thank you for your attention to this matter.

Sincerely,
Ibrahim.

David Sanders

unread,
Apr 5, 2023, 1:12:48 AM4/5/23
to django-d...@googlegroups.com
Hi Ibrahim,

get_FOO_display() isn't intended to be overridden like that, it's just a convenience method for use in templates/whatever that refers to the underlying flatchoices.

For clarity, please see the documentation: https://docs.djangoproject.com/en/4.2/ref/models/instances/#django.db.models.Model.get_FOO_display

Kind regards,
David

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/4f432f39-b959-422e-b062-5db54722b18en%40googlegroups.com.

Ibrahim Abou Elenein

unread,
Apr 5, 2023, 5:04:45 AM4/5/23
to Django developers (Contributions to Django itself)
Isn't this some sort of duplication? why not just use it instead of writing its logic again?

David Sanders

unread,
Apr 5, 2023, 5:51:31 AM4/5/23
to django-d...@googlegroups.com
At this point I'll let others chime in with their opinion on whether this is something that needs to change because:
  1. I rarely use admin
  2. I've never really had the need to override a choice's display over those supplied via `choices`
:)
Reply all
Reply to author
Forward
0 new messages