我在从多个外键提取信息并将该信息显示在 django 管理中时遇到了一些困难。我有四个模型:主题、研究、程序和事件。前三个是最后一个的外键。我希望每个事件的信息在事件管理中显示,如下所示:last_name、first_name、ssn、study_desc、procedure_desc 和 event_start_time 其中,last_name、first_name_ 和 ssn 位于主题模型中,study_desc 位于研究模型中,procedure_desc 是位于Procedure 模型中,event_start_time 位于Event 模型中。
到目前为止,我已经能够通过使用模型表单将主题模型和事件模型中的信息一起显示,但我未能成功地从其他两个模型中获取附加信息以与我现在所拥有的一起显示。任何建议、见解或替代方法将不胜感激。我使用的表格如下。
class EventForm(ModelForm):
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['subject'].queryset = \
Subject.objects.all().order_by('last_name')
class Meta:
model = Event
class EventAdmin(admin.ModelAdmin):
form = EventForm
search_fields = ['subject__last_name','subject__first_name','subject__ssn']
list_display = ['last_name','first_name','ssn','event_start_time']
I've been having some difficulty pulling information from multiple foreign keys and getting that information to display in the django admin. I have four models: Subject, Study, Procedure, and Event. The first three are foreign keys to the last. I want information from each to display in the admin for Event as such: last_name, first_name, ssn, study_desc, procedure_desc, and event_start_time where last_name, first_name_ and ssn are located in the Subject model, study_desc is located in the Study model, procedure_desc is located in the Procedure model and event_start_time is located in the Event model.
Thus far, I've been able to have the information from the Subject model and the Event model display together through use of a Model Form, but I have been unsuccessful in getting additional information from the other two models to display with what I have now. Any suggestions, insights, or alternate methods to use would be much appreciated. The form that I used is below.
class EventForm(ModelForm):
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['subject'].queryset = \
Subject.objects.all().order_by('last_name')
class Meta:
model = Event
class EventAdmin(admin.ModelAdmin):
form = EventForm
search_fields = ['subject__last_name','subject__first_name','subject__ssn']
list_display = ['last_name','first_name','ssn','event_start_time']
发布评论
评论(1)
显示相关对象信息的一种选择是使用
可调用
。ModelAdmin
的 >list_display 文档。另请检查此问题关于 SO 的一些详细示例和讨论。
One option to display information from related objects is to use a
callable
.This is exlained in the
list_display
docs for Django'sModelAdmin
.Also check this question on SO for some detailed examples and discussion.