在管理员中显示来自信号的自定义消息 [英] Display custom message from signal in the admin

查看:54
本文介绍了在管理员中显示来自信号的自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预保存的信号监听器,可以更新第二个模型.与此示例相同:

I have a pre-save signal listener that updates a second model. The same as this example:

我想让用户知道侦听器成功更新了模型并提供了一些信息.通常,我认为我可以使用django具有的内置消息功能.问题在于信号无法访问请求".所以我看不到如何使用内置的Django Messages Framework.

I'd like to let the user know that the listener succeeded in updating the model and provide some information. Normally, I would think I could use the built in messages functionality that django has. The problem is that the signal doesn't have access to 'request'. So I can't see how to use the built in Django Messages Framework.

是否存在用于向管理员中的用户发送消息的已知方法?也许通过覆盖其中一个模型的save()方法?(发送信号或接收信号的人),但我也不认为save()方法也可以访问请求"吗?

Is there a known method for sending a message to the user in the admin? Maybe by overriding the save() method for one of the models? (the one sending the signal, or receiving), but I don't think the save() method has access to 'request' either?

这一定是其他人也想做的吗?

This must be something others want to do as well?

推荐答案

您可以覆盖 save_model 方法.像这样:

You can override save_model method in ModelAdmin. Something like this:

from django.contrib import messages
# your imports
...
# your code

def save_model(self, request, obj, form, change):
    obj.user = request.user  
    obj.save()
    # you can just call super(YourModelAdminName, self).save_model(request, obj, form, change)
    messages.add_message(request, messages.INFO, 'Text of message')

这篇关于在管理员中显示来自信号的自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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