如何仅查询未决的利息而不查询已经接受/拒绝的利息? [英] How to query only the pending interest and not interest that is already accepted/declined?

查看:40
本文介绍了如何仅查询未决的利息而不查询已经接受/拒绝的利息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅查询待处理的利息,而不查询已经接受/拒绝的利息?目前,我能够查询已提交的兴趣数量.如何仅查询状态为未决且不接受/拒绝视图的兴趣?

How to query only the pending interest and not interest that is already accepted/declined? Currently, i am able to query the number of interests that has been submitted. How can I query only the interest that has the status on pending and not accept/decline for my views?

我试图做 total_interests = blog_post.interest_set.status.pending.count()但出现AttributeError ..'RelatedManager'对象没有属性'status'

I tried to do total_interests = blog_post.interest_set.status.pending.count() but got AttributeError..'RelatedManager' object has no attribute 'status'

models.py

models.py

class BlogPost(models.Model):
 title                  = models.CharField(max_length=50, null=False, blank=False, unique=True)
 author                     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
 slug                   = models.SlugField(blank=True, unique=True)

class Interest(models.Model):
   user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
   blog_post = models.ForeignKey(BlogPost, on_delete=models.CASCADE)

class InterestInvite(models.Model):

   ACCEPT = "ACCEPT"
   DECLINE = "DECLINE"
   PENDING = "PENDING"
   STATUS_CHOICES = [
      (ACCEPT, "accept"),
      (DECLINE, "decline"),
      (PENDING, "pending"),

   ]

   interest = models.OneToOneField(Interest, on_delete=models.CASCADE, related_name="interest_invite")   
   status = models.CharField(max_length=25, choices=STATUS_CHOICES, default=PENDING)


views.py

class BlogPostMixin(object):
    model=BlogPost

class DetailBlogPostView(BlogPostMixin,DetailView):
    template_name = 'HomeFeed/detail_blog.html'

   def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        blog_post=self.get_object()
        total_interests = blog_post.interest_set.count()
        context['total_interests'] = total_interests

推荐答案

您可以查询和过滤到 Interest InterestInvite ,如下所示:

You can query and filter to Interest and InterestInvite like the following:

获取状态为 ACCEPTED BlogPost 对象blog_post的所有兴趣,例如:

Get all interests of a BlogPost object blog_post with status ACCEPTED like:

blog_post.interest_set.filter(interest_invite__status=InterestInvite.ACCEPT)

状态为 PENDING BlogPost 对象blog_post的全部兴趣计数,例如:

Or a count of all interests of a BlogPost object blog_post with status PENDING like:

blog_post.interest_set.filter(interest_invite__status=InterestInvite.PENDING).count()

这篇关于如何仅查询未决的利息而不查询已经接受/拒绝的利息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆