8

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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
meesterguyperson
  • 1,726
  • 1
  • 20
  • 31
  • If you are only concerned with string fields, you could always make it blank=True, null=False which would give you an empty string there (what you want). – Two-Bit Alchemist Jan 27 '15 at 16:11
  • Unfortunately, there are foreign keys involved. Hoping for a more general solution. – meesterguyperson Jan 27 '15 at 16:16
  • possible duplicate of [How to change EMPTY\_CHANGELIST\_VALUE in Django Admin?](http://stackoverflow.com/questions/18324030/how-to-change-empty-changelist-value-in-django-admin) – Two-Bit Alchemist Jan 27 '15 at 16:28
  • 1
    I peeked at the django.contrib.admin source code and discovered that this is a hard-coded constant. Searching for that constant name gave me the answer to your question, which is unfortunately that you have to do some ugly hack like monkey patch or invoke the translation system just for this purpose. I recommend alexce's solution in the linked answer. – Two-Bit Alchemist Jan 27 '15 at 16:29
  • 1
    As an aside, this might be a candidate for raising a bug with the Django devs. This seems like a very legitimate use case and it's very unfortunate there is no real API to override this default. – Two-Bit Alchemist Jan 27 '15 at 16:30
  • Appreciate your taking the time. May have to do that. Will see if I can manage to make the monkey patch work for now. Thanks. – meesterguyperson Jan 27 '15 at 16:34

3 Answers3

24

Starting with Django 1.9 this behavior has changed and some custom functionality was added to support a solution to the situation you describe.

With Django 1.9, Django now uses a '-' (dash) instead of "(None)" to show NULLs in the Django admin.

In addition you can now customize this default '-' (dash) display for the Django admin with the empty_value_display : globally, for a specific admin class or a specific field:

Globally:

# In settings.py to show '???' instead of '-' for all null django admin values
from django.contrib import admin
admin.site.empty_value_display = '???'

For all fields in a Django admin class:

# admin.py to show "Unknown Item field" instead of '-' for null values in all Item fields
class ItemAdmin(admin.ModelAdmin):
    empty_value_display = 'Unknown Item field'

For a single field:

# admin.py to show "No known price" instead of '-' just for null price values in Item
class ItemAdmin(admin.ModelAdmin):
    list_display = ('name','price_view')
    def price_view(self, obj):
         return obj.price
    price_view.empty_value_display = 'No known price'
10

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.

Charlie
  • 8,530
  • 2
  • 55
  • 53
meesterguyperson
  • 1,726
  • 1
  • 20
  • 31
0

For example, there is name field which accepts a None(Null) value in Person model as shown below. *You should use str(self.name) instead of self.name as shown below otherwise there is an error when you save a None(Null) value in Django Admin:

# "models.py"

class Person(models.Model):                            # Here
    name = models.CharField(max_length=20, blank=True, null=True)

    def __str__(self):
        return str(self.name) # Here

Then, you can use AdminSite.empty_value_display, ModelAdmin.empty_value_display, @admin.display()'s empty_value or view_name.empty_value_display below to show -empty- for a None(Null) value on change list page in Django Admin:

# "admin.py"

admin.site.empty_value_display = "-empty-" # Or

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    list_display = ("name", "view_name")
    empty_value_display = "-empty-" # Or

    @admin.display(empty_value="-empty-") # Or
    def view_name(self, obj):
        return obj.name

    view_name.empty_value_display = '-empty-' # Or

Then, -empty- is shown when you save a None(Null) value as shown below:

enter image description here

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129