我在模型上有一个属性,我不希望其他开发人员能够进入Django shell并进行更改。有人知道怎么做吗?我尝试覆盖该模型上的save方法,但我无法确定该属性是否已更改。
答案 0 :(得分:0)
好吧,我想出了如何实现这一目标。另一位开发人员总是可以更改代码,但这会引发一个错误,说这不是他们本应该做的。
class myModel(models.Model):
uuid = UUIDField('UUID', primary_key=True, default=uuid4)
model_type = models.ForeignKey(ModelType)
# override the Press model __init__ method to store initial press_type
def __init__(self, *args, **kwargs):
super(myModel, self).__init__(*args, **kwargs)
self.__model_type = self.model_type
# override the save method to prevent updates to press_type
def save(self, *args, **kwargs):
# raise an exception if press_type was changed since initialized
if self.pk and self.__model_type != self.model_type:
raise Exception('The model_type field cannot be changed once set.')
super(myModel, self).save(*args, **kwargs)