如何防止“更改成功"覆盖 save_model 方法时的消息 [英] How to prevent "Changed successfully" message when overriding save_model method

查看:22
本文介绍了如何防止“更改成功"覆盖 save_model 方法时的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户在管理员中查看记录,但不保存任何内容.所以我正在尝试做这样的事情:

I'm trying to let users view records in the admin but not save anything. So I'm trying to do something like this:

def save_model(self, request, obj, form, change):
    """Override save method so no one can save"""
    messages.error(request, "No Changes are permitted from this screen."
        " To edit projects visit the 'Projects' table, and make sure you are"
        " in the group 'Visitor_Program_Managers'.")

这可行,但是我在下一个屏幕上收到两条消息:

This works however I get two messages on the next screen:

我在上面的信息首先然后是...已成功更改."

My message above first And then a "The ... was changed successfully."

如何阻止第二条消息?

推荐答案

您可以隐藏某些消息并显示其他消息.在您的情况下,您有兴趣不显示成功消息.您可以执行以下操作:

You can hide certain messages and show others. In your case, you are interested in not displaying the the success message. You can do the following:

def save_model(self, request, obj, form, change):
    messages.set_level(request, messages.ERROR)
    messages.error(request, 'No changes are permitted ..')

这篇关于如何防止“更改成功"覆盖 save_model 方法时的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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