I have a model called Question and another one called Answer.
class Question(models.Model):
text = models.CharField(max_length=140)
class Answer(models.Model):
user = models.ForeignKey(User)
question = models.ForeignKey(Question)
uncertain = models.BooleanField()
text = models.CharField(max_length=30)
Now I'd like to have a QuerySet questions
which is equivalent to Question.objects.all()
but including the currently logged in user's answers. I'm almost completely sure that this could be accomplished by an explicit JOIN
in SQL, but I'm certain there is a better way to do this using Django's ORM.
So how would I do that?
EDIT: To clarify, I'd like to be able to loop through the QuerySet, e.g. for q in questions
and then be able to see whether or not the user has answered it through something like q.answered
which could be a boolean, or q.answer
which could be the Answer object itself (or None
).