Custom Fields
Explore how to add custom fields that are not part of your Django model in the admin list view, including creating functions for computed values and displaying colored text. Learn to add clickable buttons linking to other admin pages or URLs to improve user interaction in the Django Admin interface.
We'll cover the following...
Adding custom fields
So far you have seen that you can use list_display to display model fields in the list page of the admin. But it is also possible to display a custom field that does not exist in the model.
For instance, if you want to display a new field in a new column to indicate that a question has been published or will be published in the future, you can create a has_been_published function inside the QuestionAdmin class and also add this property to the list_display list:
list_display = ('question_text', 'refAuthor', 'has_been_published', 'pub_date', 'createdDate', 'updatedDate',)
list_display_links = ('question_text', 'refAuthor',)
def has_been_published(self, obj):
present = datetime.now()
return ...