如何在 django-simple-history 中引用history_list_display中的外键?



我已经将 HistoricalRecords 添加到 django 中的一个模型中。

我能够使用history_list_display属性在管理面板的历史记录页面中显示列。

在这些列中,我有 employee 可以使用history_list_display属性元组中的employee显示。但是当我尝试使用employee__person__person_name对其他表进行外键引用时,它显示None.

如何在 django admin的历史记录页面中显示外键引用值?

django-simple-history保存对象的历史版本时,它通过删除其外键上的任何外键约束来实现(这样当您删除历史记录指向的对象时,历史记录不受影响(。按照django-simple-history处理外键的方式,你不能做你在 django admin 中通常能够做的双下划线查询类型。相反,您可以自己查询对象,如下所示:

@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

最新更新