Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
django-immutablemodel
Advanced tools
Easily make all of a django model's fields immutable after saving. You can customize it to make some fields mutable. You can change when it can be mutable (e.g. whenever the field's value is nil, or only when a particular 'lock field' is false)
Very heavily based on Rob Madole's original code at https://bitbucket.org/robmadole/django-immutablefield and Helder Silva's fork at https://bitbucket.org/skandal/django-immutablefield. Rob's was 'inspired by a Google search that didn't turn up reusable solution for making fields immutable inside of a Django model'.
Pulled across from hg/bitbucket to git/github because less headache as they are more familar to me (Tim)
One of the following:
Via the ole' standby::
easy_install django-immutablemodel
Pip::
pip install django-immutablemodel
To install directly from Github::
pip install git+https://github.com/red56/django-immutablemodel
.. hint:: You do not need to add anything into Django's INSTALLED_APPS
Allows you to declare a Django model as immutable.
It works as a drop-in replacement for Django's own Model
. This means you
can ImmutableModel
.
::
from django.db import models
from immutablemodel.models import ImmutableModel
CruiseShip(ImmutableModel):
name = models.CharField(max_length=50)
class Meta:
mutable_fields = [] # you can actually leave this out...
Now you can try with all your might, but once you've saved it won't change (within reason, sure this is Python we can do almost anything if we try hard enough)
::
>>> queen_anne = CruiseShip.objects.create(name='Queen Anne')
<CruiseShip 'Queen Anne'>
>>> queen_anne.name = 'King George'
>>> queen_anne.name
'Queen Anne'
Change the meta section to include immutable_quiet = False
and it will raise a
ValueError
if an attempt is made to change this value
::
class Meta:
mutable_fields = [] # you can actually leave this out...
immutable_quiet = False
The error is raised as soon as you try and set the field, not when save()
is
called.
::
>>> queen_anne = CruiseShip.objects.create(name='Queen Anne')
<CruiseShip 'Queen Anne'>
>>> queen_anne.name = 'King George'
ValueError: name is immutable and cannot be changed
If you want you can make ALL immutable fields complain by adding
IMMUTABLE_QUIET=False
to your settings.py
List the fields you actually want mutable in "mutable_fields"
::
CruiseShip(ImmutableModel):
name = models.CharField(max_length=50)
passengers = models.PositiveIntegerField()
class Meta:
mutable_fields = ['passengers']
Please note that fields beginning with an underscore are ignored by ImmutableModel - this allows immutable_lock_field to be a @property (ie. they are automatically mutable - thanks to https://github.com/Bouke for contributing a patch for this -- see https://github.com/red56/django-immutablemodel/pull/1)
Meta
Specify options (in addition to the normal django model's Meta options) that
control how immutable fields are handled when
subclassing the ``ImmutableModel`` class
``mutable_fields``
Tell ``ImmutableModel`` which fields should be allowed to change.
This value must be a tuple or a list and contain the names of the fields
as strings.::
class Meta:
mutable_fields = ['some_transient_data']
Specify multiple fields::
class ImmutableMeta:
mutable_fields = ['some_transient_data', 'name', 'foreign_key']
``immutable_fields``
Tell ``ImmutableModel`` which fields should not be allowed to change.
NB: you can't specify mutable_fields AND immutable_fields.
This value must be a tuple or a list and contain the names of the fields
as strings.::
class Meta:
immutable_fields = ['my_special_id']
Specify multiple fields::
class ImmutableMeta:
immutable_fields = ['my_special_id', 'name', 'foreign_key']
``immutable_quiet``
If an attempt is made to change an immutable field, should we quietly
prevent it.
Set this value to ``False`` to raise a ``ValueError`` when an immutable
field is changed.::
class ImmutableMeta:
immutable_quiet = False
``immutable_lock_field``
This determines when to enforce immutability. By default it is equal to immutable_model.models.PK_FIELD.
This means that when the PK_FIELD is full (typically when saved) the model is immutable, but before it is
saved it is mutable.
Alternatively you can specify a field by name, or you can set it to None, which means that you can't change
immutable fields once they are set (even before saving).
class ImmutableMeta:
immutable_lock_field = ['is_locked']
settings.py
``IMMUTABLE_QUIET``
Set this to ``False`` to make all immutable_fields raise an Exception when attempting
to be changed.
.. This is your project NEWS file which will contain the release notes. .. Example: http://www.python.org/download/releases/2.6/NEWS.txt .. The content of this file, along with README.rst, will appear in your .. project's PyPI page.
Release date: 28-Apr-2010
FAQs
A base class for Django to allow immutable fields on Models
We found that django-immutablemodel demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.