ModelForm ve Meta.fields Tanımlama
Birkaç satırda bir formu modele bağlayın.
ModelForm ve Meta.fields Tanımlama, CoddyKit'te ücretsiz bir Django Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Django Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Django Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Why ModelForm Exists
You already have a model describing your data. A ModelForm builds a form straight from that model, so you never repeat the same fields twice. 🎯
Import ModelForm
ModelForms live in the same place as regular forms. You subclass forms.ModelForm instead of forms.Form to get the model-aware version.
from django import forms
from .models import PostThe Meta Class
A ModelForm needs an inner Meta class. Meta is where you tell the form which model it is built from and which fields to include.
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ["title", "body"]Point at a Model
The model attribute names the class your form mirrors. Django reads that model's fields to decide which form inputs to generate for you.
class Meta:
model = PostListing Fields Explicitly
The fields list controls exactly which model fields become form inputs. Naming them keeps the form intentional and safe from surprises.
fields = ["title", "body", "published"]The Shortcut: fields = all
Setting fields to the string "__all__" includes every model field. It is handy in demos but risky, since new model fields appear automatically.
class Meta:
model = Post
fields = "__all__"Excluding Instead
You can flip it with exclude to keep everything except a few fields. Prefer an explicit fields list when you can, for clarity.
class Meta:
model = Post
exclude = ["author"]Auto-Generated Widgets
Django picks a sensible widget for each field automatically. A CharField becomes a text box and a BooleanField becomes a checkbox without extra work.
Validation Comes Free
Your model's rules ride along too. A field marked required or with a max length brings that same validation into the form for free.
Using It in a View
In a view you create the form the same way as any form. An empty PostForm() renders blank inputs ready for the user to fill.
def create(request):
form = PostForm()
return render(request, "create.html", {"form": form})Binding to an Instance
Pass an existing object with instance and the form pre-fills its values. The same ModelForm now powers both creating and editing.
form = PostForm(instance=post)Quick Check
Where do you tell a ModelForm which model and fields to use?
Recap: ModelForm Basics
Nice work! A ModelForm builds a form from a model using an inner Meta with model and fields. You skip repetition and inherit validation for free. ✨
Yapay zeka eğitmeniyle Python öğren — ücretsiz
Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.
- Kurslar
- 30
- Dersler
- 120
Sıkça Sorulan Sorular
“ModelForm ve Meta.fields Tanımlama” dersi ücretsiz mi?
Evet — “ModelForm ve Meta.fields Tanımlama” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Django Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Django Academy kursu toplamda 4 dersten oluşur.
“ModelForm ve Meta.fields Tanımlama” dersinde ne öğreneceğim?
Birkaç satırda bir formu modele bağlayın. Django Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Django Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Django Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“ModelForm ve Meta.fields Tanımlama” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Django Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Django Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- ModelForm ve Meta.fields Tanımlama
- form.save() ve commit=False
- clean Yöntemleriyle Özel Doğrulama
- Widget'lar, Etiketler ve Yardım Metni