Admin Stacked Inline and Tabular Inline Django.How

Author avatar wrote on 07/06/2022

Both allow you to edit models on the same page as a parent model.

In other words, it is sometimes preferable for a user to have the possibility to edit a certain model
while editing another one instead of having to manually add another instance somewhere else in your interface.

The difference is Layout

TabularInline

class GlossaryTermLanguageInline(admin.TabularInline):
    model = GlossaryTermLanguage
    fk_name = 'segment'  # must add if more than 1 FK
    # exclude = ("e",)  # optional
    # list_display = ('pk', 'name')
    # readonly_fields = ('id',)
    # can_delete = False
    # classes = ['collapse']
    # verbose_name_plural = 'Translation Files'

class GlossaryTermAdmin(admin.ModelAdmin):
    inlines = [GlossaryTermLanguageInline]

admin.site.register(GlossaryTerm, GlossaryTermAdmin)

Another example

from django.contrib import admin

from .models import Question, Choice

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3

class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [(None, {'fields': ['question_text']}),
    ('Data Information', {'fields': ['pub_date'], 'classes': ['collapse']}),]

    inlines = [ChoiceInline]


admin.site.register(Question, QuestionAdmin)

StackedInline

class FileTranslationInline(admin.StackedInline):
    model = FileTranslation
    list_display = ('pk', 'name')
    readonly_fields = ('id',)
    can_delete = False
    classes = ['collapse']
    verbose_name_plural = 'Translation Files'
    fk_name = 'source_file'


class FileStatisticsInline(admin.StackedInline):
    model = FileStatistics
    classes = ['collapse']
    fk_name = 'source_file'


class FileAdmin(admin.ModelAdmin):
    inlines = (FileTranslationInline, FileStatisticsInline)


admin.site.register(File, FileAdmin)

We stand with Ukraine