Display ForeignKey data on admin change / add page in Django

I am trying to get the model attribute displayed on the admin change / add page in Django of another model. Here are my models:

class Download(model.Model): task = models.ForeignKey('Task') class Task(model.Model): added_at = models.DateTimeField(...) 

It is not possible to switch the foreign key, so I cannot use Inlines, and of course fields = ('task__added_at',) does not work either.

What is the standard approach to something like this? (or am I stretching the admin too much?)

I already use a custom template, so if this is an answer that can be made. However, I would prefer to do this at the administrator level.

+4
source share
1 answer

If you do not need to edit it, you can display it as a reading field:

 class DownloadAdmin(admin.ModelAdmin): readonly_fields = ('task_added_at',) def task_added_at(self, obj): return obj.task.added_at 
+9
source

Source: https://habr.com/ru/post/1348119/


All Articles