首页 > 解决方案 > 如果发送者和接收者不相同,Django Count 不能在信号中工作

问题描述

如果我的博客模型中的发送者和接收者不相同,则我的 base.html 中未显示计数。我在我的模型中使用信号。Count 适用于我的 blogcomment 模型,但 如果发送者和接收者不同,它不适用于我的博客模型。如果我使用sender = blog.author然后 receiver = blog.author它工作和计数。看图片了解一下: 在此处输入图像描述

这是我的models.py #this model for author

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....
      #here signals stating 
      def blog_notify(sender, instance, *args, **kwargs):
          blog = instance
          blog_title = blog.title
          #sender =blog.author #only count working if sender and receiver is same
          sender = User.objects.filter(username="admin1")    
          receiver = blog.author

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

          if sender == blog.author and blog.is_published == "pending":
             notify = Notifications(blog=blog, sender=sender, receiver=receiver,text_preview = blog_title[:250], notification_type="pending post")
             notify.save()

          
          if sender == blog.author and blog.is_published == "rejected":
             notify = Notifications(blog=blog, sender=sender,receiver=receiver,text_preview = blog_title[:250], notification_type="post rejected")
             notify.save()


#this model for comment user. if anyone comment on blog post
class BlogComment(models.Model):
      blog = models.ForeignKey(Blog,on_delete=models.CASCADE,null=True, blank=True)
      #my others fields....
      #here signals stating 
       def user_comment(sender, instance, *args, **kwargs):
            comment= instance
            blog = comment.blog
            sender = comment.user
            commnet_notes = comment.rejected_comment_notes
            comment_text = comment.comment
            
            if sender != blog.author and comment.is_published == "pending":
               notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment")
               notify.save()

            if sender != blog.author and comment.is_published == "published":
                notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="Comment Approved")
                notify.save()
      
            
            if sender != blog.author and comment.is_published == "rejected":
                notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250],help_notes=commnet_notes, notification_type="Comment Rejected")
                notify.save()
      
        
      
post_save.connect(BlogComment.user_comment, sender=BlogComment)  
post_save.connect(Blog.blog_notify, sender=Blog)  

views.py用于在我的模板中显示评论数量

def Count_Notifications(request):
    count_notifications_comment = 0
    count_notifications_author = 0
    blog_author = None
    
    if request.user.is_authenticated:
        count_notifications_comment = Notifications.objects.filter(sender=request.user,is_seen=False).count()
        count_notifications_author = Notifications.objects.filter(receiver=request.user,is_seen_author_noti=False).count()
        print('#########',count_notifications_author)
        blog_author = Blog.objects.filter(author=request.user)
    return {'count_notifications_comment':count_notifications_comment,'count_notifications_author':count_notifications_author,'blog_author':blog_author} 

如果发送者和接收者不同,我不明白为什么 count 现在适用于博客模型。

标签: pythondjangodjango-queryset

解决方案


推荐阅读