Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

https://secure.travis-ci.org/gavinwahl/django-optimistic-lock.png?branch=master

Implements an offline optimistic lock [1] for Django models.

Usage

Add a VersionField and inherit from VersionedMixin.

from dol import VersionField, VersionedMixin

class MyModel(VersionedMixin, models.Model):
    version = VersionField()

Whenever MyModel is saved, the version will be checked to ensure the instance has not changed since it was last fetched. If there is a conflict, a ConcurrentUpdate exception will be raised.

Implementation

A VersionField is just an integer that increments itself every time its model is saved. VersionedMixin overrides _do_update (which is called by save to actually do the update) to add an extra condition to the update query – that the version in the database is the same as the model’s version. If they match, there have been no concurrent modifications. If they don’t match, the UPDATE statement will not update any rows, and we know that someone else saved first.

This produces SQL that looks something like:

UPDATE mymodel SET version = version + 1, ... WHERE id = %s AND version = %s

When no rows were updated, we know someone else won and we need to raise a ConcurrentUpdate.

Comparison to django-concurrency

django-concurrency before version 0.7 used SELECT FOR UPDATE to implement the version checking. I wanted to avoid database-level locking, so django-optimistic-lock adds a version filter to the update statement, as described by Martin Fowler [1].

Additionally, ool takes a more minimalistic approach than django-concurrency by only doing one thing – optimistic locking – without any monkey-patching, middleware, settings variables, admin classes, or form fields. django-concurrency would probably make more sense if you’re looking for something that will attempt to accommodate every situation out of the box. Use ool if you just want a straightforward model implementation and need to handle the UI and surrounding architecture yourself.

Running the tests

make test

Metadata

Release files for djongo-optimistic-lock 1.0.1.dev0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djongo-optimistic-lock 1.0.1.dev0
File Size Uploaded
djongo-optimistic-lock-1.0.1.dev0.tar.gz 9.2 kB Details

Release files / djongo-optimistic-lock-1.0.1.dev0.tar.gz

Download URL djongo-optimistic-lock-1.0.1.dev0.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1b584069a7bd5e5e2274f6e77344446d36a4ec0f8c5a048e191832c76ace5d1d
BLAKE2b-256 checksum
How to use checksums
9aae7608258dc599d1d7bd520fd5f42b515c5bf1586e40b8f808c6852461dd0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.11

Release history Release notifications | RSS feed

This release

1.0.1.dev0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page