Django signals how to add admin user as reciver ?

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:

models.py

class Blog(models.Model):
      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")
             notify.save()

post_save.connect(Blog.blog_notify, sender=Blog)  

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?

Is the above expression a reference to a User object?

Also, your system could have multiple admin accounts. Your code needs to be able to handle that situation.

1 Like

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

How do you query any table for finding specific entries? (See the docs on Making Queries if necessary.)

I tried to use this query in my signals admin_user = Blog.objects.filter(author=instance.is_superuser) for getting admin user but still now same error

You’re looking to query on User, not Blog. You want a list of users for whom the is_superuser attribute is True.

How would you write a query to get all of the admin users from the User table?

1 Like

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

From your original question:

So your query is right. As I wrote earlier:

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.

1 Like

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:

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")

    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_to_user")
#others fields......

I also tried this:
user = User.objects.all().get(is_superuser=True) and getting this error get() returned more than one User -- it returned 3!

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.

Here is my full code:

#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()

So as written, receiver is a ForeignKey - that means it can only refer to one User.

That gives you two basic choices -

  • Use a loop and generate a separate instance of the Notification for each recipient
  • Change the receiver field to be a ManyToMany so that it can refer to multiple User.
    • This may require a change to your signal code. I don’t see that in this post, so I can’t be sure.
1 Like

KenWhitesell Thanks for the solution and find out my mistake.