django允许模型上不可变字段的基类
django-immutablemodel的Python项目详细描述
很容易使django模型的所有字段在保存后都不可变。 您可以自定义它以使某些字段可变。 你可以改变什么时候它是可变的 (例如,当字段值为零时,或仅当特定的“锁定字段”为false时)
历史和学分
很大程度上基于rob madole在https://bitbucket.org/robmadole/django-immutablefield和 希尔瓦的叉子在https://bitbucket.org/skandal/django-immutablefield。 Rob's的灵感来自于一个谷歌搜索,它没有提供可重复使用的解决方案 django模型中不可变的字段。
从hg/bitbucket转到git/github,因为它们对我来说更亲密无间(tim)
安装
下列之一:
通过OLE“待机:
easy_install django-immutablemodel
管道:
pip install django-immutablemodel
直接从github安装:
pip install git+https://github.com/red56/django-immutablemodel
提示
你不需要在django的INSTALLED_APPS中添加任何内容
它做什么
允许您将django模型声明为不可变的。
它可以替代django自己的Model。这意味着你 可以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...
现在你可以尽力尝试,但是一旦你保存了它就不会改变(在合理的范围内, 当然这是python,只要我们努力,我们几乎可以做任何事情)
>>> queen_anne = CruiseShip.objects.create(name='Queen Anne') <CruiseShip 'Queen Anne'> >>> queen_anne.name = 'King George' >>> queen_anne.name 'Queen Anne'
你可以让它抱怨
将元节更改为包含immutable_quiet = False,它将引发 ValueError如果试图更改此值
class Meta: mutable_fields = [] # you can actually leave this out... immutable_quiet = False
只要您尝试设置字段,就会引发错误,而不是在save()为 打电话。
>>> queen_anne = CruiseShip.objects.create(name='Queen Anne') <CruiseShip 'Queen Anne'> >>> queen_anne.name = 'King George' ValueError: name is immutable and cannot be changed
如果需要,可以通过添加 IMMUTABLE_QUIET=False到您的设置.py
您可以使某些字段可变
在“可变字段”中列出您实际需要可变的字段
CruiseShip(ImmutableModel): name = models.CharField(max_length=50) passengers = models.PositiveIntegerField() class Meta: mutable_fields = ['passengers']
请注意,ImmutableModel忽略了以下划线开头的字段-这允许Immutable\u lock\u字段是@property (即,由于https://github.com/Bouke为此提供了修补程序,它们是自动可变的–请参见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 ^{tt3}$ class
^{tt9}$
Tell ^{tt3}$ 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 ^{tt3}$ 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 ^{tt14}$ to raise a ^{tt5}$ when an immutable field is changed.:
class ImmutableMeta: immutable_quiet = Falseimmutable_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
^{tt17}$
Set this to ^{tt14}$ to make all immutable_fields raise an Exception when attempting to be changed.
发行说明
0.1
发布日期:2010年4月28日
- Rob Madole以Django ImmutableField的身份首次发布
0.2
- 由Helder Silva(Skandal)在Bitbucket上编写的版本,在字段外添加符号。(未正式发布)
0.3
- 由tim diggins(red56)编写的版本,称之为django immutablemodel,并在默认情况下保存后使其成为不变性
0.3.1条 -
-
用抽象模型< /LI>固定问题
0.3.2条 -
- 允许在meta中完全继承不变性选项
0.3.3条 -
- 缺省锁字段中不可变的(不可删除的)模型的保存问题,保存后不能删除(因为Django删除收集器需要改变ID字段)
0.3.4条 -
- 使以“始终可变”开头的字段(不可变模型忽略此字段)