Reputation: 61
I am a newbie to Django, I want to make quiz app but I am stuck in the problem. I have created 3 models(Quiz, Question, Choice). I want to write a function which returns questions which have the same quiz title.
I tried this
views.py
def detail(request):
sets = Quiz.objects.all()
question = Question.objects.filter(sets.title)
return render(request,'App/appdetail.html',{'question':question})
models.py
class Quiz(models.Model):
title = models.CharField(max_length=20)
description = models.CharField(max_length=100)
def __str__(self):
return self.title
class Question(models.Model):
set = models.ForeignKey(Quiz,on_delete=models.CASCADE)
question_txt = models.CharField(max_length=100)
def __str__(self):
return self.question_txt
class Choice(models.Model):
question = models.ForeignKey(Question,on_delete=models.CASCADE)
choice_txt = models.CharField(max_length=20)
boolean = models.BooleanField(default=False)
def __str__(self):
return self.choice_txt
Error Message
Upvotes: 1
Views: 433
Reputation: 7330
You can try like this:
def detail(request,pk):
quiz = get_object_or_404(Quiz,pk=pk)
questions = quiz.question_set.all()
return render(request,'App/appdetail.html',{'questions':questions,'quiz':quiz})
Upvotes: 0
Reputation: 13057
You can get all the questions with same quiz title by filtering questions on quiz foreignKey set
in your Question
model.
question = Question.objects.filter(set__title='your_quiz_title')
Upvotes: 1