Package: django-immutablemodel
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)History and creditsVery heavily based on Rob Madole’s original code athttps://bitbucket.org/robmadole/django-immutablefieldand
Helder Silva’s fork athttps://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)InstallingOne of the following:Via the ole’ standby:easy_install django-immutablemodelPip:pip install django-immutablemodelTo install directly from Github:pip install git+https://github.com/red56/django-immutablemodelHintYoudo notneed to add anything into Django’sINSTALLED_APPSWhat does it doAllows you to declare a Django model as immutable.It works as a drop-in replacement for Django’s ownModel.  This means you
canImmutableModel.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'You can make it complainChange the meta section to includeimmutable_quiet = Falseand it will raise aValueErrorif an attempt is made to change this valueclass Meta:
    mutable_fields = [] # you can actually leave this out...
    immutable_quiet = FalseThe error is raised as soon as you try and set the field, not whensave()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 changedIf you want you can make ALL immutable fields complain by addingIMMUTABLE_QUIET=Falseto your settings.pyYou can make some fields mutableList 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 tohttps://github.com/Boukefor contributing a patch for this – seehttps://github.com/red56/django-immutablemodel/pull/1)ReferenceMetaSpecify options (in addition to the normal django model’s Meta options) that
control how immutable fields are handled when
subclassing theImmutableModelclassmutable_fieldsTellImmutableModelwhich 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_fieldsTellImmutableModelwhich 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_quietIf an attempt is made to change an immutable field, should we quietly
prevent it.Set this value toFalseto raise aValueErrorwhen an immutable
field is changed.:class ImmutableMeta:
    immutable_quiet = Falseimmutable_lock_fieldThis 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.pyIMMUTABLE_QUIETSet this toFalseto make all immutable_fields raise an Exception when attempting
to be changed.Release notes0.1Release date: 28-Apr-2010Initial release by Rob Madole as django-immutablefield0.2Version by Helder Silva (skandal) on bitbucket adding sign_off field. (not formally released)0.3Version by Tim Diggins (red56) calling it django-immutablemodel and making immutability after save by default0.3.1
—Fixing problem with abstract models0.3.2
—Allowing for full inheritance of immutability options within Meta0.3.3
—Fixing problem where immutable (non undeletable) models with default lock_field couldn’t be deleted after save (because of Django deletion collector needing to change id field)0.3.4
—Making fields starting with _ always mutable (ignored by immutable model)