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

ModelForm की मूल बातें

ModelForm एक shortcut है जो एक database model से अपने आप एक form बनाता है, ताकि आपको हर field hand से फिर से type न करना पड़े।
Syntax
markup
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.

markup
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 रखना चाहते थे।

Warning: Sensitive या internal-only fields वाले models के लिए fields को explicitly list करना prefer करें।

उदाहरण: Using __all__ Carefully

fields = __all__ includes every model field, which is convenient but can expose fields you meant to keep internal.

markup
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.

markup
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. #}
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. ModelForm को generate करने देने के बजाय manually वह हर field फिर से declare करना जो model के पास already है।
  2. Meta class की fields list भूल जाना, जिसका मतलब है form में silently zero fields रह जाते हैं।
  3. Sensitive models से जुड़े एक form पर fields = __all__ उपयोग करना, accidentally उन fields को expose करते हुए जिन्हें hidden रहना चाहिए।
चैप्टर सारांश
  • ModelForm एक model की field definitions से अपने आप form fields generate करता है।
  • एक Meta inner class specify करती है कि कौन सा model और कौन से fields include करने हैं।
  • ModelForm आपके models.py और forms.py के बीच duplication कम करता है।

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.