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

ModelForm Data को Save करना

एक ModelForm save करना एक ऐसा button click करने जैसा है जो दोनों आपके answers check करे और उन्हें सीधे database में file कर दे।
Syntax
markup
# Create
form = FormName(request.POST)
if form.is_valid():
    obj = form.save()

# Update
form = FormName(request.POST, instance=obj)

एक नया Instance Save करना

एक valid, किसी instance से unbound ModelForm पर save() call करना एक बिल्कुल नई database row बनाता है।

उदाहरण: Saving a New Instance

Calling save() on a valid, unbound-to-instance ModelForm creates a brand new database row.

markup
form = ArticleForm(request.POST)
if form.is_valid():
    form.save()
{# 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. #}

एक मौजूदा Instance Update करना

instance=article pass करना form को एक मौजूदा row से bind करता है, इसलिए save() एक नई row बनाने के बजाय इसे update करता है।

उदाहरण: Updating an Existing Instance

Passing instance=article binds the form to an existing row, so save() updates it instead of creating a new one.

markup
form = ArticleForm(request.POST, instance=article)
if form.is_valid():
    form.save()
{# 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. #}

Save को Delay करना

commit=False model instance को database में लिखे बिना return करता है, आपको पहले extra fields set करने देते हुए।

उदाहरण: Delaying the Save

commit=False returns the model instance without writing to the database yet, letting you set extra fields first.

markup
form = ArticleForm(request.POST)
if form.is_valid():
    article = form.save(commit=False)
    article.author = request.user
    article.save()
{# 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. पहले form.is_valid() check किए बिना form.save() call करना, जो invalid data पर एक error raise करता है।
  2. यह मान लेना कि form.save() हमेशा database में लिखता है, तब भी जब commit=False pass किया गया हो।
  3. commit=False उपयोग करते समय final save() से पहले instance पर extra fields (जैसे logged-in user) set करना भूल जाना।
चैप्टर सारांश
  • एक valid ModelForm का save() method एक model instance create या update करता है और इसे एक ही call में database में लिखता है।
  • commit=False pass करना unsaved instance return करता है ताकि आप save करने से पहले extra fields set कर सकें।
  • save() से पहले हमेशा is_valid() call करें — एक invalid form save करना एक error raise करता है।

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.