[Django]-Django duplicate database queries

4👍

You should .prefetch_related(..) [Django-doc] the related Choices. Then Django will make an extra query to fetch the Choices all at once, and do the JOIN at the Python/Django level.

def index(request):
    return render(
        request,
        'polls/index.html',
        {'questions': Question.objects.prefetch_related('choice_set')}
    )

Leave a comment