django - 在 django 信号中使用多个实例和发件人
问题描述
我如何在 django 信号中使用多个实例和发送者(不同的模型)。
receiver(post_save, sender=Comment)
@receiver(post_save, sender=Post)
def create_comment_notification(*args, **kwargs):
#comments notifier
comment = kwargs['instance']
post = kwargs['instance']
if kwargs['created']:
if comment.author != comment.post_connected:
Notification.objects.create(
assigned_to = post.author,
group='NC',
body=f"{comment.author} commented: {comment.content} on your post.",
pk_relation=comment.id
)
else:
if comment.author != comment.post_connected:
Notification.objects.create(
assigned_to = post.author,
group='NC',
body=f"{comment.author} commented: {comment.content}.",
pk_relation=comment.id
)
我注意到它可以工作,但不能识别 POST 模型,它只使用 COMMENT。我如何将这两个模型用作发件人。并让代码分别识别它们。
谢谢
解决方案
推荐阅读
- python - Python openCV - 编译的 .exe 文件在不同的计算机上不起作用
- regex - Vim 替换一行子字符串中的几个相似元素
- c++ - 保存文件对话框以从数字数组中保存 txt
- javascript - UseEffect 在没有焦点反应导航的情况下工作
- c# - 在 C# .NET 中的异步方法中将项目异步添加到列表中
- python - 如何以“\n”也是python中的新元素的方式进行拆分?
- php - 尽管保存在数据库和文件夹 Laravel 中,但图像未显示
- javascript - Django 和 DRF-DataTables - 修复过滤行为
- sql - SQL Sum 正在计算一个唯一的班次,而不是整个星期
- powerbi - Powerbi:希望根据切片器选择在第三个可视化中显示两个值的差异