← Back to Django Course | Chapter 4: Models & Django ORM Basics | Lesson 8 of 10

Model Instances Update करना

Updating cabinet से एक card निकालने, एक पुराने answer को काटने, नया लिखने, और card को वापस रखने जैसा है।
Syntax
markup
# One object
obj = ModelName.objects.get(id=1)
obj.field = new_value
obj.save()

# Many rows
ModelName.objects.filter(field=value).update(field=new_value)

एक Single Object Update करना

.get() से object fetch करें, एक या ज़्यादा attributes को normal Python attributes जैसे बदलें, फिर change को वापस लिखने के लिए .save() call करें।

उदाहरण: Updating a Single Object

Fetch the object with .get(), change one or more attributes like normal Python attributes, then call .save() to write the change back.

bash
book = Book.objects.get(id=1)
book.pages = 300
book.save()

⚠️ Run this in your own terminal or Node.js environment.

एक साथ कई Rows Update करना

एक QuerySet पर directly .update() call करना हर matching row को एक database operation में बदल देता है, उन्हें पहले Python में load किए बिना।

Note: यह हर object पर individually .save() call करके loop करने से कहीं तेज़ है।

उदाहरण: Updating Many Rows at Once

Calling .update() directly on a QuerySet changes every matching row in one database operation, without loading them into Python first.

bash
Book.objects.filter(genre='Action').update(is_featured=True)

⚠️ Run this in your own terminal or Node.js environment.

save() vs update()

save() एक already-fetched single instance पर काम करता है और कोई भी custom save logic या signals चलाता है। update() directly एक QuerySet पर काम करता है और speed के लिए per-instance logic skip करता है।

Warning: update() हर object की .save() method call नहीं करता, इसलिए इसके लिए post_save signals fire नहीं होंगे।

उदाहरण: save() vs update()

save() works on a single already-fetched instance and runs any custom save logic or signals. update() works directly on a QuerySet and skips per-instance logic for speed.

bash
book = Book.objects.get(id=2)
book.title = 'New Title'
book.save()

Book.objects.filter(id=3).update(title='Bulk Title')

⚠️ Run this in your own terminal or Node.js environment.

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. एक fetched instance पर एक attribute बदलना लेकिन .save() call करना भूल जाना, इसलिए change कभी database तक नहीं पहुंचता।
  2. .filter(...).update(...) उपयोग करना लेकिन model .save() signals जैसे post_save के fire होने की expect करना — वे नहीं होते, क्योंकि update() .save() bypass करता है।
  3. एक field update करने के लिए एक object fetch करना, यह realize न करते हुए कि update_fields save को ज़्यादा precise और efficient बना सकता था।
चैप्टर सारांश
  • एक object update करने के लिए, इसे fetch करें, इसके attributes बदलें, फिर .save() call करें।
  • QuerySet.update(...) कई matching rows को directly database में एक साथ update कर सकता है।
  • .save() पूरी row फिर से लिखता है; .update() bulk में सिर्फ specified fields बदलता है।
  • .update() से Bulk updates तेज़ हैं लेकिन हर object की .save() method और signals skip करते हैं।

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.