开发者

List foreign keys linking to a model

开发者 https://www.devze.com 2022-12-08 05:13 出处:网络
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

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 "Manufact开发者_C百科urer" and another one for "Model"...the "Model" model links to "Manufacturer" via a foreign key.


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.

0

精彩评论

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