ModelForm Data को Save करना
# 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.
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.
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.
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. #}
- पहले
form.is_valid()check किए बिनाform.save()call करना, जो invalid data पर एक error raise करता है। - यह मान लेना कि
form.save()हमेशा database में लिखता है, तब भी जबcommit=Falsepass किया गया हो। commit=Falseउपयोग करते समय finalsave()से पहले instance पर extra fields (जैसे logged-in user) set करना भूल जाना।
- एक valid ModelForm का
save()method एक model instance create या update करता है और इसे एक ही call में database में लिखता है। commit=Falsepass करना unsaved instance return करता है ताकि आप save करने से पहले extra fields set कर सकें।save()से पहले हमेशाis_valid()call करें — एक invalid form save करना एक error raise करता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: