List of foreign keys associated with the model

How can I display objects that reference an object via a ForeignKey in Django (especially in the admin frontend). For example, if I click on an object, I not only see the object, but any other object that references it. eg I have a model for "Manufacturer" and another one for "Model" ... the model "Model" refers to "Manufacturer" via a foreign key.

+2


source to share


1 answer


You can achieve this using inline lines.

In your case, where each Model

has a Manufacturer

given foreign key, first create an inline class for Model

, then add it to the class ManufacturerAdmin

.

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



class ModelInline(admin.StackedInline):
    model = Model

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

admin.site.register(Manufacturer, ManufacturerAdmin)

      

The Django docs contain information on possible settings.

+3


source







All Articles