How to show related objects in Django/Admin?

前端 未结 3 1411
野的像风
野的像风 2021-02-07 02:19

I have 2 models:

from django.db import models

class Category(models.Model):
    icon = models.ImageField(upload_to = \'thing/icon/\')
    image = models.ImageFi         


        
3条回答
  •  耶瑟儿~
    2021-02-07 02:38

    You can use "Inlines" to visualize and edit Things of a certain Category in the admin detail for that category:

    In the admin.py file, create an Inline object for Thing (ThingInline) and modify your CategoryAdmin class to have an inline of type ThingInline like this:

    ...
    class ThingInline(admin.TabularInline):
        model = Thing
    
    class CategoryAdmin(admin.ModelAdmin):
        inlines = [
            ThingInline,
        ]
    ...
    

    For further details, this is the docs for admin inlines: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

提交回复
热议问题