在 django admin 中显示多个外键的信息

发布于 2024-11-07 17:08:35 字数 922 浏览 0 评论 0 原文

我在从多个外键提取信息并将该信息显示在 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']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不打扰别人 2024-11-14 17:08:35

显示相关对象信息的一种选择是使用可调用

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's ModelAdmin.

Also check this question on SO for some detailed examples and discussion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文