Django - 禁用模型编辑 [英] Django - disable model editing

查看:106
本文介绍了Django - 禁用模型编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不是说要删除保存,而是删除从模板保存并继续按钮 - 应该没有可以更改模型的已提交实例的值的操作。



最好是选择另存为选项。

解决方案

覆盖模型的保存功能,如下所示:

 
class MyModel(models.Model):

def save(self,* args,** kwargs):

如果self.pk为无:
super(MyModel,self).save(* args,** kwargs)

此函数只调用超类保存功能(实际上保存更改),如果没有pk,例如模型实例是新的。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆