![]() |
VOOZH | about |
When Django model instances are created, they appear in the admin interface with generic labels like ModelName object (1). This default naming can make it difficult to identify specific records when multiple entries exist.
Giving model instances clear, readable names makes them easier to identify and manage.
Example: Consider a project named 'geeksforgeeks' having an app named 'geeks'.
In models.py:
After creating this model, we need to run two commands in order to create Database for the same:
python manage.py makemigrations
python manage.py migrate
After migrations and creating an instance with "GfG is Best" as the geeks_field value, the admin will display it as GeeksModel object (1). This default label is not descriptive and makes it hard to identify the entry.
👁 display name django modelsTo make objects more readable in the admin, add a __str__ method to the model. This method should return a descriptive string representing the object.
In models.py:
Creating an object with geeks_field = "GfG is Best" will display "GfG is Best" in the admin instead of the generic "GeeksModel object (1)".
👁 django-object-name-display