pythondjangonulldjango-adminchangelist

In the Django admin changelist, how to show a blank space instead of the default "(None)"?


When a field is null in the database, Django inserts "(None)" to hold the place of the null when displaying a changelist. While descriptive, when there are lots of fields on a changelist, it makes everything very busy to look at, where as a blank field would be just as helpful but much less cluttered. So is there some way to change the text Django uses for representing null fields in the changelist? Doesn't matter if the solution is modeladmin-specific or admin wide.

Should also add that I am aware of the solution where you define custom fields and then output a blank string. That works, but in makes the column unsortable, and that is a priority before display, so it's not an option.


Solution

  • You can override the individual ModelAdmin behavior with this workaround:

    from django.contrib.admin.views import main
    ...
    ...
    ...
    class MyModelAdmin(admin.ModelAdmin):
        def __init__(self,*args,**kwargs):
            super(MyModelAdmin, self).__init__(*args, **kwargs)
            main.EMPTY_CHANGELIST_VALUE = '-'
    

    Note: Overriding the __init__() is incompatable with the @admin.register() decorator, instead you will need to call admin.site.register(Model, ModelAdmin) after the Model class is defined.