[Fixed]-Which query to be used in data base

1👍

You can do:

# Get a Question instance. Your implementation may be different. 
question = Question.objects.get(pk=question_pk)
correct_option = question.options_set.filter(is_answer=True).first()

Doing question.options_set.filter(is_answer=True) returns a queryset of Options instances that have a FK to the question instance so doing .first() returns the first instance in the queryset.

You can also get a particular option (given “Only one option for each question is Set to True”):

try:    
    question.options_set.get(is_answer=True)
except Options.DoesNotExist:
    raise

Leave a comment