Django - 禁用模型编辑 [英] Django - disable model editing
本文介绍了Django - 禁用模型编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在不破坏管理员权限的情况下禁用在 ORM 级别上编辑现有模型实例?
我不是在谈论从模板中删除保存"和保存并继续"按钮 - 不应该有可以更改模型已提交实例的值的操作.
最好使用另存为"选项.
解决方案
像这样覆盖模型的保存函数:
<上一页>类 MyModel(models.Model):def 保存(自我,*args,**kwargs):如果 self.pk 为无:super(MyModel, self).save(*args, **kwargs)如果没有pk,这个函数只调用超类save函数(实际上是保存更改),例如模型实例是新的.
Is there a way, hopefully without breaking admin, to disable editing existing model instances on the ORM level?
I'm not talking about removing 'Save' and 'Save and continue' buttons from templates - there should be no operations that can change the values of a committed instance of a model.
Preferably, the 'Save As' option should work instead.
解决方案
Overwrite the save function for your model like so:
class MyModel(models.Model): def save(self, *args, **kwargs): if self.pk is None: super(MyModel, self).save(*args, **kwargs)
This function only call the superclass save function (which actually saves the change) if there is no pk, e.g. the model instance is new.
这篇关于Django - 禁用模型编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文