0

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?

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

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

1 Answers1

0

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

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

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

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()
Marco
  • 2,371
  • 2
  • 12
  • 19
  • Hey Marco could you check out this question :) https://stackoverflow.com/questions/65879639/how-do-i-include-my-def-clean-slug-function-into-my-views-or-template-so-that-it –  Jan 25 '21 at 11:45