I am building a simple question and answer site and I am trying to implement a functionality in which user can upload 4 answers But I am trying to hide show only any one answer
And this is showing all the answers related to a question.
For Example :- question_a has 4 answers then i am trying to show any one of them.
models.py
class Question(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=30)
marked = models.BooleanField(booleanField)
class Answer(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
question_of = models.ForeignKey(BlogPost, on_delete=models.CASCADE)
body = models.CharField(max_length=30)
views.py
def page(request):
query_2 = Answer.objects.filter(question_of__marked=True)[:1]
context = {'query_2':query_2}
return render(request, 'page.html', context)
In this query I am trying to show Answers of qustions which have marked=True
and i am also trying to show only one answer of a question But it is showing 4.
And after using [:1] it is only showing result in all.
- I also tried
.distinct()
but it made no effect on results.
What i am trying to do ?
I am trying to show first answer of every question that has marked=True
I will really appreciate your Help. Thank You
CodePudding user response:
This will take the first row from the query
query_2 = Answer.objects.filter(question_of__marked=True).first()
CodePudding user response:
The answer model has a one to many relationship with the blogPost model not the question model change that and it should work.