0

I have added HistoricalRecords to a model in django.

I was able to display columns in history page in admin panel using history_list_display attribute.

In those columns, I have employee which can be displayed using employee in the history_list_display attribute tuple. But when I try to do ForeignKey reference using employee__person__person_name to other tables, it is displaying None.

How can I display ForeignKey reference values in history page in django admin?

Underoos
  • 4,708
  • 8
  • 42
  • 85

1 Answers1

0

When django-simple-history saves historical versions of your objects, it does so by removing any foreign key constraints on its foreign keys (this is so that when you delete an object that an historical record points to, the historical record is unaffected). Do the way django-simple-history handles foreign keys, you cannot do the type of double underscore querying you would normal be able to do in django admin. Instead, you can query for the object yourself, as demonstrated below:

@register(Employee)
class EmployeeAdmin(SimpleHistoryAdmin):
    list_display = [...]
    history_list_display = ["get_employee_name", "get_employee_manager_name"]

    def get_employee_name(self, obj):
        obj.person.name

    def get_employee_manager_name(self, obj):
        return Employee.objects.filter(pk=obj.employee.id).first().manager.person.person_name
Ross Mechanic
  • 424
  • 3
  • 11