views:

122

answers:

1

How can I display objects that link to an object via a ForeignKey in Django (specifically in the admin interface). For example, if I click on an object, I'll not only see the object but also any other object that link to it. e.g. I have a model for "Manufacturer" and another one for "Model"...the "Model" model links to "Manufacturer" via a foreign key.

+2  A: 

You can achieve this using inlines.

In your case, where each Model has a Manufacturer defined by a foreign key, first create an inline class for Model, then add it to your ManufacturerAdmin class.

The admin.py file for your application should look something like:

class ModelInline(admin.StackedInline):
    model = Model

class ManufacturerAdmin(admin.ModelAdmin)
    inlines = [
        ModelInline,
    ]

admin.site.register(Manufacturer, ManufacturerAdmin)

The Django docs contains details about possible customizations.

Alasdair