开发者

Displaying information from multiple foreign keys in the django admin

开发者 https://www.devze.com 2023-03-05 22:51 出处:网络
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.

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 additi开发者_高级运维onal 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']


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消