ModelForm की मूल बातें
class FormName(forms.ModelForm):
class Meta:
model = ModelName
fields = ['field1', 'field2']
एक ModelForm बनाना
एक ModelForm का Meta class एक model को point करता है और बताता है कि कौन से fields को form fields में बदलना है।
उदाहरण: Creating a ModelForm
A ModelForm's Meta class points at a model and lists which fields to turn into form fields.
from django import forms
from .models import Article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ['title', 'body']
{# 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. #}
`__all__` सावधानी से उपयोग करना
fields = __all__ हर model field include करता है, जो convenient है लेकिन उन fields को expose कर सकता है जिन्हें आप internal रखना चाहते थे।
उदाहरण: Using __all__ Carefully
fields = __all__ includes every model field, which is convenient but can expose fields you meant to keep internal.
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = '__all__'
{# 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. #}
इसके बजाय Fields Exclude करना
exclude उन fields को list करता है जिन्हें छोड़ना है, बाकी हर model field को form पर अपने आप रखते हुए।
उदाहरण: Excluding Fields Instead
exclude lists fields to leave out, keeping every other model field on the form automatically.
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
exclude = ['created_at']
{# 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. #}
- ModelForm को generate करने देने के बजाय manually वह हर field फिर से declare करना जो model के पास already है।
Metaclass की fields list भूल जाना, जिसका मतलब है form में silently zero fields रह जाते हैं।- Sensitive models से जुड़े एक form पर
fields = __all__उपयोग करना, accidentally उन fields को expose करते हुए जिन्हें hidden रहना चाहिए।
- ModelForm एक model की field definitions से अपने आप form fields generate करता है।
- एक
Metainner class specify करती है कि कौन सा model और कौन से fields include करने हैं। - ModelForm आपके
models.pyऔरforms.pyके बीच duplication कम करता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: