Django Update Data
Updating a row in Django means fetching the existing instance, changing its fields in Python, and calling save() to write the change back.
Fetch, Mutate, Save
The standard update pattern has three steps: use get() to fetch a single matching row, assign new values to its fields like any Python object, then call save() to persist the change with an UPDATE statement.
Example
from .models import Product
product = Product.objects.get(pk=3)
product.price = 19.99
product.in_stock = False
product.save()Updating Many Rows at Once
Calling update() directly on a queryset runs a single SQL UPDATE against every matching row, without loading each object into Python. It's much faster than looping and calling save() on each instance, but it does not call save() or send signals.
Example
from django.db.models import F
# Give every out-of-stock product a 10% price cut, in one query
Product.objects.filter(in_stock=False).update(price=F('price') * 0.9)get_or_create() and update_or_create()
These shortcuts combine a lookup with a write. update_or_create() fetches a row matching the given fields and updates it with defaults, or creates a new row if none exists. Both return a (object, created) tuple, where created is a boolean.
Example
product, created = Product.objects.update_or_create(
name='Wireless Mouse',
defaults={'price': 22.50, 'in_stock': True},
)Exercise: Django Update Data
What is the typical pattern for updating a single existing record via a model instance?