What's the difference between `.save()` and `.update()`?
The difference between Django model save and queryset update, shown through the SQL each emits. save() acts on an instance already in memory, issues an UPDATE listing every concrete field of that one row, and runs the whole model layer on the way: a save override, presave and postsave signals, and autonow timestamps. update() acts on the database, compiling the queryset filter plus the named columns into a single UPDATE across every matching row, returning the number of rows matched without fetching anything, constructing any instance, calling save, or firing a signal. Explains why that makes update immune to the read-modify-write race and ideal with F expressions, and why an instance held in memory goes stale after an update and can overwrite it if saved afterwards, with refreshfromdb as the fix. Includes runnable Django code and real output, plus save(updatefields), sliced querysets, and auto_now not being applied.
Comentarii