Skip to main content

Module ops

Module ops 

Source
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 (save may be two, see below). save sends pre_save before and post_save (with the created flag) after it; delete sends pre_delete before and post_delete after a row was removed. Receivers are those connected to Db::signals; see signals for ordering and failure rules. A failing pre_* receiver aborts the operation; a failing post_* receiver errors after the statement ran, so wrap the call in Db::transaction to roll it back. Use Db::on_commit for work that must run only after the surrounding transaction commits. Bulk QuerySet writes send no signals.
  • Atomicity. A single statement is atomic. save on a manual primary key runs UPDATE and, if no row matched, INSERT; outside a transaction another writer can slip in between the two, so wrap concurrent upserts in Db::transaction (or use QuerySet::get_or_create).
  • Timestamps. auto_now_add columns are stamped on insert and auto_now columns on every save, with the current UTC time.
  • Stored state wins. save reads the stored row back (RETURNING *) and replaces *self, so database defaults, generated keys and stamped timestamps are visible immediately. Cached related objects of ForeignKey fields are dropped by that replacement.

Traits§

ModelOps
Persistence operations available on every Model.