Django 如何为获取所有管理员用户编写查询集并将它们用作接收者的信号
Django how to write queryset for get all admin user and use them in signals as receiver
我使用 django 信号的目的是在任何作者创建新博客时通知每个管理员 post。所以我想使用所有管理员作为接收者,而创建博客 post 的唯一作者将是发件人。目前我正在使用这个 User.objects.get(username='jhone' )#jhone is an admin user
查询集来设置接收者特定的管理员用户。如何调用所有管理员用户并将主题全部设置为接收者。这是我的代码:
#models.py
#using notifications model for signals
class Notifications(models.Model):
blog = models.ForeignKey('blog.Blog',on_delete=models.CASCADE)
blogcomment = models.ForeignKey('blog.BlogComment',on_delete=models.CASCADE, blank=True,null=True)
NOTIFICATION_TYPES = (('New Comment','New Comment'),('Comment Approved','Comment Approved'), ('Comment Rejected','Comment Rejected'),('pending post','pending post'),('post approved','post approved'),('post rejected','post rejected'))
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_from_user")
receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_to_user")
#others fields......
class Blog(models.Model):
author = models.ForeignKey(User,on_delete=models.CASCADE,max_length=100)
#others fields....
def blog_notify(sender, instance, *args, **kwargs):
blog = instance
blog_title = blog.title
sender = blog.author
receiver = User.objects.get(username='jhone')
if sender == blog.author and blog.is_published == "published":
notify = Notifications(blog=blog, sender=sender,receiver =receiver ,text_preview = blog_title[:250], notification_type="post approved")
notify.save()
post_save.connect(Blog.blog_notify, sender=Blog)
我也尝试了几个查询集来将所有管理员用户设置为接收者,但非主题无效。
receiver = User.objects.all().filter(is_superuser=True)
我收到这个错误:
Cannot assign "<QuerySet [<User: Jhone>, <User: admin1>, <User: admin2>]>": "Notifications.user" must be a "User" instance.
使用 User.objects.filter(is_superuser=True)
这个查询集。
我也试过这个
receiver = User.objects.all().get(is_superuser=True)
并收到此错误
MultipleObjectsReturned at /blog-admin/approve-blog-post/tusar-post2/
get() returned more than one User -- it returned 3!
知道如何传递正确的查询集以将所有管理员用户设置为接收者吗???
您需要遍历所有管理员并像这样一一创建 Notifications
:
def blog_notify(sender, instance, *args, **kwargs):
blog = instance
blog_title = blog.title
sender = blog.author
receivers = User.objects.filter(is_superuser=True)
if not (sender == blog.author and blog.is_published == "published"):
return
notifications = []
for receiver in receivers:
notifications.append(Notifications(blog=blog, sender=sender, receiver=receiver, text_preview=blog_title[:250], notification_type="post approved"))
Notifications.objects.bulk_create(notifications)
请注意,如果 Notifications
模型正在发送信号,则您不能使用 bulk_create
。在那种情况下,只需一个一个地创建它们。
我使用 django 信号的目的是在任何作者创建新博客时通知每个管理员 post。所以我想使用所有管理员作为接收者,而创建博客 post 的唯一作者将是发件人。目前我正在使用这个 User.objects.get(username='jhone' )#jhone is an admin user
查询集来设置接收者特定的管理员用户。如何调用所有管理员用户并将主题全部设置为接收者。这是我的代码:
#models.py
#using notifications model for signals
class Notifications(models.Model):
blog = models.ForeignKey('blog.Blog',on_delete=models.CASCADE)
blogcomment = models.ForeignKey('blog.BlogComment',on_delete=models.CASCADE, blank=True,null=True)
NOTIFICATION_TYPES = (('New Comment','New Comment'),('Comment Approved','Comment Approved'), ('Comment Rejected','Comment Rejected'),('pending post','pending post'),('post approved','post approved'),('post rejected','post rejected'))
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_from_user")
receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_to_user")
#others fields......
class Blog(models.Model):
author = models.ForeignKey(User,on_delete=models.CASCADE,max_length=100)
#others fields....
def blog_notify(sender, instance, *args, **kwargs):
blog = instance
blog_title = blog.title
sender = blog.author
receiver = User.objects.get(username='jhone')
if sender == blog.author and blog.is_published == "published":
notify = Notifications(blog=blog, sender=sender,receiver =receiver ,text_preview = blog_title[:250], notification_type="post approved")
notify.save()
post_save.connect(Blog.blog_notify, sender=Blog)
我也尝试了几个查询集来将所有管理员用户设置为接收者,但非主题无效。
receiver = User.objects.all().filter(is_superuser=True)
我收到这个错误:
Cannot assign "<QuerySet [<User: Jhone>, <User: admin1>, <User: admin2>]>": "Notifications.user" must be a "User" instance.
使用 User.objects.filter(is_superuser=True)
这个查询集。
我也试过这个
receiver = User.objects.all().get(is_superuser=True)
并收到此错误
MultipleObjectsReturned at /blog-admin/approve-blog-post/tusar-post2/
get() returned more than one User -- it returned 3!
知道如何传递正确的查询集以将所有管理员用户设置为接收者吗???
您需要遍历所有管理员并像这样一一创建 Notifications
:
def blog_notify(sender, instance, *args, **kwargs):
blog = instance
blog_title = blog.title
sender = blog.author
receivers = User.objects.filter(is_superuser=True)
if not (sender == blog.author and blog.is_published == "published"):
return
notifications = []
for receiver in receivers:
notifications.append(Notifications(blog=blog, sender=sender, receiver=receiver, text_preview=blog_title[:250], notification_type="post approved"))
Notifications.objects.bulk_create(notifications)
请注意,如果 Notifications
模型正在发送信号,则您不能使用 bulk_create
。在那种情况下,只需一个一个地创建它们。