1
I have a model :
class Question(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=120)
    description = models.TextField()
    answers = models.ManyToManyField('Answer',related_name='answer_name', blank=True)

Here what I want is I want to list all the question which dont have any answer.

I did:

un_list = Question.objects.annotate(a_count="Answers").filter(a_count=0)

Is it right way of doing ?

1 Answer 1

1

You're close. Here's what you probably want:

from django.db.models import Count
un_list = Question.objects.annotate(a_count=Count("answer")).filter(a_count=0)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.