0

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']
JosephS
  • 1
  • 1
  • Check this thread: http://stackoverflow.com/questions/163823/can-list-display-in-a-django-modeladmin-display-attributes-of-foreignkey-fields – arie May 13 '11 at 14:38
  • Worked perfectly, much better than what I was attempting. Thanks. – JosephS May 13 '11 at 15:45
  • @arie: probably should've posted that as an answer rather than a comment. It solves his problem, you deserve the credit for tracking down an acceptable soluton, and then the question get's marked as answered. – Chris Pratt May 13 '11 at 16:20
  • Hey. Turned my comment into an answer. I just felt that simply linking to an existing answer isn't really worth the credit. But of course i don't mind getting my answer accepted and i am glad the proposed solution was helpful. Have a nice weekend. – arie May 14 '11 at 08:09

1 Answers1

0

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.

Community
  • 1
  • 1
arie
  • 18,737
  • 5
  • 70
  • 76