django-immutablefield

A base class for Django to allow immutable fields on Models
Download

django-immutablefield Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Rob Madole
  • Publisher web site:
  • http://bitbucket.org/robmadole/

django-immutablefield Tags


django-immutablefield Description

A base class for Django to allow immutable fields on Models django-immutablefield is inspired by a Google search that didn't turn up a reusable solution for making fields immutable inside of a Django model.InstallingOne of the following:Via the ole' standby:easy_install django-immutablefieldPip:pip install django-immutablefieldTo install directly from Bitbucket:pip install -e hg+http://bitbucket.org/robmadole/django-immutablefield#egg=django-immutablefieldHintYou do not need to add anything into Django's INSTALLED_APPSWhat does it doAllows you to define certain fields as immutable inside of Django models.It works as a drop-in replacement for Django's own Model. This means you can ImmutableModel even if you don't specify ImmutableMeta.from django.db import modelsfrom immutablefield.models import ImmutableModelCruiseShip(ImmutableModel): name = models.CharField(max_length=50) class ImmutableMeta: # After ya name a ship, you can't change it matey immutable = def __unicode__(self): return u'%s' % self.nameNow you can try with all your might, but the field 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 include quiet = False and it will raise a ValueError if an attempt is made to change this valueclass ImmutableMeta: # After ya name a ship, you can't change it matey immutable = quiet = FalseThe 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 changedReferenceImmutableMeta Specify options that control how immutable fields are handled when subclassing the ImmutableModel class immutable Tell ImmutableModel which fields should not be allowed to change. This value must be a tuple or a list and contain the names of the fields as strings.: class ImmutableMeta: immutable = Specify multiple fields: class ImmutableMeta: 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 Requirements: · Python · Django What's New in This Release: · Goofed up some setuptools dependencies and easy_install was grabbing dependencies you really don't want.


django-immutablefield Related Software