如何解决Django-Postgres触发器中的RAISE EXCEPTION引起的InternalError
我是Django的新手,我需要一些帮助, 我想限制模型实例的编辑,如果X字段中有一个值,并且您尝试更改任何字段都将收到消息。
由于我使用的是postgres,所以我考虑过使用触发器
CREATE OR REPLACE FUNCTION checkear_lb()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
BEGIN
--Check
IF not (old.linea_base_id is null) THEN
RAISE EXCEPTION 'You cant edit this because';
END IF;
RETURN NULL; --I also tried with NEW
END;
$$
create trigger tarea_con_lb
before update
on "Desarrollo_tarea"
for each row execute procedure checkear_lb();
可以,但是我明白了
InternalError at /admin/Desarrollo/tarea/1/change/
You can't edit this because
CONTEXT: función PL/pgSQL checkear_lb() en la línea 5 en RAISE
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/Desarrollo/tarea/1/change/
Django Version: 3.0.3
Exception Type: InternalError
Exception Value:
You cant edit this because
CONTEXT: función PL/pgSQL checkear_lb() en la línea 5 en RAISE
Exception Location: C:\Python38\lib\site-packages\django\db\backends\utils.py in _execute,line 86
Python Executable: C:\Python38\python.exe
Python Version: 3.8.2
Python Path:
也尝试覆盖保存功能,结果相同
class Tarea(models.Model):
....
#linea base
linea_base = models.ForeignKey('LineaBase',null=True,blank=True,help_text='Linea Base',on_delete=models.SET_NULL) #,on_delete=models.CASCADE
def save(self,*args,**kwargs):
if self.linea_base:
if (self.og_version != self.version):
raise Exception('You cant edit this because')
else:
super(Tarea,self).save(*args,**kwargs)
else:
super(Tarea,**kwargs)
这显然是由触发器引起的,我想知道是否有更好的方法在django中处理它,或者只是一种处理方法。 谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。