Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you're overthinking things. Essentially what you want to know (as far as I can tell - correct me if I'm wrong) is which questions have been answered by a given user, and which questions haven't (the complement). So:</p> <pre><code>user_answered_questions = set([answer.question for answer in user.answer_set.all()]) </code></pre> <p>Then unanswered questions are everything else. So:</p> <pre><code>user_unanswered_questions = set(Question.objects.all()) - answered_questions </code></pre> <p>The point of doing it this way rather than uzi's solution is that you should use what you know sooner rather than later. If you filter the answer_set for every question by user, your initial query will be selecting every single answer and every single question in these tables. By starting from the known data (i.e. the user) we are selecting only answers and questions related to that user.</p> <p>EDIT:</p> <p>So if you then want to combine the two sets into a single list, there are a bunch of ways you could do this for instance by creating a list of tuples:</p> <pre><code>questions = [(q, q in user_answered_questions) for q in Question.objects.all()] </code></pre> <p>Then you can iterate over questions, retaining the order of the queryset:</p> <pre><code>for q in questions: print "it is %s that this user answered the \"%s\" question" % (q[1], q[0]) </code></pre> <p>It's not pretty but you get the point - you've already determined what you wanted to determine, it's just a matter of contorting it to whatever data structure you happen to need.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload