如何防止“更改成功”?覆盖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 ..')