Admin के Inlines
In this page:
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.
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.
@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 होने से रोकता है।
TabularInline के बजाय StackedInline उपयोग करें।उदाहरण: Controlling Blank Rows with extra
Shows existing Chapter rows plus exactly one blank row for adding a new one.
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. #}
- Inline के model FK को सही से set करना भूल जाना, जिससे Django startup पर एक configuration error raise करता है।
- कई rows वाले एक model के लिए
StackedInlineउपयोग करना, parent का edit page extremely लंबा बनाते हुए। extra = 0set न करना, हर edit page पर default से कई confusing blank rows छोड़ते हुए।
TabularInlinerelated rows को एक compact table में दिखाता है;StackedInlineउन्हें separate stacked forms के रूप में दिखाता है।- Inlines एक parent ModelAdmin से
inlineslist के ज़रिए attach होते हैं। extracontrol करता है कि नए related records add करने के लिए कितनी blank rows दिखें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: