Displaying object information in admin
Currently, when we look at our model objects in admin, it is hard to identify individual objects (as shown in Figure 7.3) – for example, News object (1) and News object (2):
For better readability in admin, we can customize what is displayed there – for example, if we want to display the headline for each news object instead. In /news/models.py
, add the following function in bold:
from django.db import models class News(models.Model): headline = models.CharField(max_length=200) body = models.TextField() date = models.DateField() def __str__(self): return self.headline
The __str__
method in Python represents the class objects as a string. __str__
will be called when the news objects are listed in admin. Note how readability...