← Back to Django Course | Chapter 8: Django Admin | Lesson 7 of 8

Admin के Inlines

Inlines आपको related records (जैसे एक book के chapters) को इसके parent के admin page के अंदर ही edit करने देते हैं, एक separate screen पर जाने के बजाय।
Syntax
markup
class ChildInline(admin.TabularInline):
    model = ChildModel
    extra = 1

@admin.register(ParentModel)
class ParentAdmin(admin.ModelAdmin):
    inlines = [ChildInline]

Inlines क्या Solve करते हैं

Inlines के बिना, एक Book की related Chapter rows edit करने का मतलब है हर एक के लिए separate Chapter admin page पर navigate करना। Inlines उस editing को Book के अपने edit page पर directly embed करते हैं।

उदाहरण: What Inlines Solve

Defines a compact, table-style inline for Chapter rows related to a Book.

markup
from django.contrib import admin
from .models import Book, Chapter

class ChapterInline(admin.TabularInline):
    model = Chapter
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

एक Inline को ModelAdmin से Attach करना

Inline class इसके parent के ModelAdmin से inlines attribute के ज़रिए attach होता है, अपने आप register नहीं होता।

उदाहरण: Attaching an Inline to a ModelAdmin

Now editing a Book in the admin shows its Chapter rows in an embedded table.

markup
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    inlines = [ChapterInline]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

`extra` से Blank Rows Control करना

Default से Django नए related objects बनाने के लिए कुछ empty rows जोड़ता है। extra को एक छोटे number (या 0) पर set करना form को cluttered होने से रोकता है।

Note: जब हर related row में कई fields हों जो table में अच्छी तरह fit न हों तो TabularInline के बजाय StackedInline उपयोग करें।

उदाहरण: Controlling Blank Rows with extra

Shows existing Chapter rows plus exactly one blank row for adding a new one.

markup
class ChapterInline(admin.TabularInline):
    model = Chapter
    extra = 1
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Inline के model FK को सही से set करना भूल जाना, जिससे Django startup पर एक configuration error raise करता है।
  2. कई rows वाले एक model के लिए StackedInline उपयोग करना, parent का edit page extremely लंबा बनाते हुए।
  3. extra = 0 set न करना, हर edit page पर default से कई confusing blank rows छोड़ते हुए।
चैप्टर सारांश
  • TabularInline related rows को एक compact table में दिखाता है; StackedInline उन्हें separate stacked forms के रूप में दिखाता है।
  • Inlines एक parent ModelAdmin से inlines list के ज़रिए attach होते हैं।
  • extra control करता है कि नए related records add करने के लिए कितनी blank rows दिखें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.