[Django]-Add inline model to django admin site

35👍

do something like this:

class ChannelAdmin(admin.TabularInline):
    model = Channel

class RuleAdmin(admin.ModelAdmin):
   inlines = [ChannelAdmin,]

admin.site.register(Rule,RuleAdmin)

2👍

ADD in admin.py

from .models import Post, Comment

class CommentInline(admin.StackedInline):
    model = Comment
    extra = 0

class PostAdmin(admin.ModelAdmin):
    inlines = [
        CommentInline,
    ]

admin.site.register(Post, PostAdmin)


Click on this:
result

1👍

class OrderItemInline(admin.TabularInline):
    model = OrderItem
    fields = ['image']    

class OrderAdmin(admin.ModelAdmin):
    list_display = ['id']
    list_filter = ['status']
    inlines = [OrderItemInline]

0👍

Mar, 2022 Update:

This will work:

from django.contrib import admin
from .models import Channel, Rule

class ChannelInline(admin.TabularInline):
    model = Channel

@admin.register(Rule)
class RuleAdmin(admin.ModelAdmin):
    inlines = [ChannelInline]

Leave a comment