Python

How to change Display Name of an Object in Django Admin interface?

  • Django
  • AdminInterface
  • DisplayName
  • ObjectName
  • Python

3 comments

@Coll your answer has been answered here
https://forum.pillowseed.com/topic/details/django-admin-interface-recent-object-title-and-object-name-doesnt-work/11

Coll 23
4 years ago

Unfortunately, i followed the steps and it doesn't work on mine.
class Menu(models.Model):
productName = models.CharField(max_length=255)
price = models.FloatField()
image_url = models.CharField(max_length=2083)
category = models.CharField(max_length=255)

def _str_ (self):
return self.product_name

Hi everyone!

There are plenty of resources which provide a solution to this. Here are three online solutions for additional reading:

1) https://stackoverflow.com/questions/9336463/django-xxxxxx-object-display-customization-in-admin-action-sidebar

2) https://stackoverflow.com/questions/48883868/how-to-change-table-object-name-in-django (thanks Coll for this!)

3) https://www.geeksforgeeks.org/change-object-display-name-using-__str__-function-django-models-python/

You can also try googling "change object display name in django admin" or some variations thereof.


On to the solution! One possible solution that works is the following:

In your app > models.py, once you create your model, continue your code with "def _str_ (self)" as follows:

from django.db import models

# Create your models here.
class Item(models.Model):
product_name = models.CharField(max_length=45)
price = models.FloatField()
category = models.CharField(max_length=10)
image_url =models.CharField(max_length=255)

# changes object display name from object to product name
def __str__ (self):
return self.product_name

Note how the second part isn't a separate code but part of the model class itself. Now the Django Admin interface will return 'product_name' rather than 'object'.