Expand description
Instance operations: ModelOps gives every Model save, delete
and refresh (Django Model.save() / delete() / refresh_from_db()).
§Semantics
- One statement, signals around it. Each operation is a single SQL
statement (
savemay be two, see below).savesendspre_savebefore andpost_save(with thecreatedflag) after it;deletesendspre_deletebefore andpost_deleteafter a row was removed. Receivers are those connected toDb::signals; seesignalsfor ordering and failure rules. A failingpre_*receiver aborts the operation; a failingpost_*receiver errors after the statement ran, so wrap the call inDb::transactionto roll it back. UseDb::on_commitfor work that must run only after the surrounding transaction commits. BulkQuerySetwrites send no signals. - Atomicity. A single statement is atomic.
saveon a manual primary key runsUPDATEand, if no row matched,INSERT; outside a transaction another writer can slip in between the two, so wrap concurrent upserts inDb::transaction(or useQuerySet::get_or_create). - Timestamps.
auto_now_addcolumns are stamped on insert andauto_nowcolumns on every save, with the current UTC time. - Stored state wins.
savereads the stored row back (RETURNING *) and replaces*self, so database defaults, generated keys and stamped timestamps are visible immediately. Cached related objects ofForeignKeyfields are dropped by that replacement.