I am using django signals for notifications. I create an signals for notify author when any new blog post will be created or update post status and I am using author both as sender and receiver. I want the author will be sender and all admin will be receiver. How to do that?
here is my code:
author = models.ForeignKey(User,on_delete=models.CASCADE,max_length=100)
title = models.CharField(max_length=300,unique=True)
#my others fields......
def blog_notify(sender, instance, *args, **kwargs):
blog = instance
blog_title = blog.title
sender = blog.author
if sender == blog.author and blog.is_published == "published":
notify = Notifications(blog=blog, sender=sender, reciver=sender,text_preview = blog_title[:250], notification_type="post approved")
I aslo tried reciver = blog.author.is_superuser for set receiver as admin but sender and receiver still author. How to add admin as receiver in my signals?
getting this error Cannot assign "False": "Notifications.user" must be a "User" instance. after using blog.author.is_superuser I also have multiple admin user. I don’t know how to call an super user in my models so I can pass it to signals
now I am using User query set sender = User.objects.filter(is_superuser=True) I aslo tried this sender = User.objects.all().filter(is_superuser=True) but none of theme didn’t work and getting this error Cannot assign "<QuerySet [<User: tusar>, <User: admin1>, <User: admin2>]>": "Notifications.user" must be a "User" instance. where I am doing mistake? I think am calling all admin user from this query
So it’s up to you to decide how you’re going to handle this. Part of that is knowing what your Notifications object does, and whether or not it can be made to handle a queryset instead of a single object as the receiver.
My purpose of this notification is notify every admin when any author create new blog post. So I want to use all admin as a reciver and the only author who creating the blog post will be sender. As you said my above quaryset right so what do you think why my signals not accepting multiple admin as reciver ??? or how to overcome this situation? My system have multiple admin. Admin1, Admin2, tusar they all are admin
You’re passing that queryset into something called Notifications. In general, you’d need to allow Notifications to accept a queryset. Without seeing what Notifications is, I can’t be any more specific than that.
KenWhitesell almost solved the problem. by using this User query User.objects.get(username='tusar') I can send notification to specific admin user but the problem is I can’t sent more then one admin user. here is my Notifications model:
KenWhitesell? by using this User query User.objects.get(username='tusar') I can send notification to specific admin user but the problem is I can’t sent more then one admin user. See my previous reply for details
Ok, to have any chance of providing effective answers to your questions, you need to show all the relevant code. This includes the complete Notifications object along with whatever other functions or classes are involved with doing this.