Inlines und schreibgeschützte Felder
Bearbeiten Sie verbundene Datensätze inline und schützen Sie Felder.
Inlines und schreibgeschützte Felder ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Edit Related Rows Together
Sometimes related records belong on the same page. An inline lets you edit child rows right inside their parent's edit form. 🧩
Two Inline Styles
Django offers two layouts. A TabularInline shows rows in a compact table, while StackedInline stacks each row as its own block.
Define an Inline
Create a small class that subclasses TabularInline and points its model attribute at the child model you want to embed.
class CommentInline(admin.TabularInline):
model = CommentAttach to the Parent
List your inline on the parent's admin. The inlines option tells Django to render those child rows inside the parent form.
class PostAdmin(admin.ModelAdmin):
inlines = [CommentInline]Spare Blank Rows
Inlines show empty rows for quick adding. Set extra to control how many blank slots appear, often just one is enough.
extra = 1The FK Requirement
Inlines only work when the child has a ForeignKey to the parent. That link is how Django knows which rows belong together.
Protect a Field
Some values should be visible but never edited by hand. Add them to readonly_fields and the admin shows them locked. 🔒
readonly_fields = ("created", "updated")Auto Timestamps
Fields like an auto_now_add date are set by Django, not people. Marking them readonly avoids confusing, unsaveable edits.
Show a Computed Value
readonly_fields can name a method too, letting you display a calculated value that has no real database column.
def word_count(self, obj):
return len(obj.body.split())Readonly Inside Inlines
Inlines accept readonly_fields as well. Use it to display child data clearly while keeping sensitive columns uneditable.
Permissions Still Apply
Readonly is about the form, not security. Real access is governed by user permissions, so combine both for safe editing.
Quick Check
What is required for a model to appear as an inline?
Recap
You embedded related rows with inlines and locked sensitive values using readonly_fields. Together they make admin editing rich yet safe. ✅
Häufig gestellte Fragen
Ist die Lektion „Inlines und schreibgeschützte Felder“ kostenlos?
Ja — der vollständige Text von „Inlines und schreibgeschützte Felder“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Inlines und schreibgeschützte Felder“?
Bearbeiten Sie verbundene Datensätze inline und schützen Sie Felder. Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Inlines und schreibgeschützte Felder“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- createsuperuser und anmelden
- Models mit admin.site registrieren
- ModelAdmin anpassen
- Inlines und schreibgeschützte Felder