Skip to main content

Practical Django model utilities.

Project description

django-openutils

Practical Django model utilities. Currently ships ModelDiffMixin for tracking field changes.

Installation

pip install django-openutils

ModelDiffMixin

A model mixin that tracks field changes via __dict__ snapshotting. Uses concrete field attnames (e.g. author_id not author) so FK fields are compared by PK value without extra DB queries. Deferred fields are excluded from tracking.

Usage

from django.db import models
from django_openutils.mixins import ModelDiffMixin


class MyModel(ModelDiffMixin, models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
obj = MyModel.objects.create(name="Alice", email="alice@example.com")
obj.has_unsaved_changes  # False

obj.name = "Bob"
obj.has_unsaved_changes  # True
obj.unsaved_fields       # dict_keys(['name'])
obj.get_unsaved_field_diff("name")  # ('Alice', 'Bob')

obj.save()
obj.has_unsaved_changes  # False
obj.has_updated_fields   # True
obj.recent_updates       # {'name': ('Alice', 'Bob')}

API

Property / Method Description
unsaved_changes Dict of {field: (old, new)} for in-memory changes not yet saved
has_unsaved_changes True if any fields differ from the last save/load
unsaved_fields Keys of unsaved_changes
get_unsaved_field_diff(field) Returns (old, new) tuple or None
recent_updates Dict of {field: (old, new)} from the last save() call
has_updated_fields True if recent_updates is non-empty
updated_fields Keys of recent_updates

License

MIT - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_openutils-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_openutils-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file django_openutils-0.1.0.tar.gz.

File metadata

  • Download URL: django_openutils-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_openutils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 51d6a04bb5f1506952cc67d45699bc0eb4e1d9e2de1f3c5faa39b0daf68f263e
MD5 aad30a2b04604d7add3f1de90caab685
BLAKE2b-256 bdcd52b81297306ce477001c5f9db034a40797808d272acef194f0782a8d0089

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_openutils-0.1.0.tar.gz:

Publisher: publish.yml on frankapps-io/django-openutils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_openutils-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_openutils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b32cee59d8f915f0e04e3db5a150af22d333570c763b33055074ef500203a37f
MD5 eaa108a4f0bc33b5ca1fd6723ed8e9e7
BLAKE2b-256 c70e9c8cfa6401979a85c23279f17f5bb4fababbf8cc695f583ea99a3dc8a289

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_openutils-0.1.0-py3-none-any.whl:

Publisher: publish.yml on frankapps-io/django-openutils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page