Select the election

This commit is contained in:
Tom Hubrecht 2020-12-20 02:31:56 +01:00
parent 7c6dfa1073
commit f4a2d1fb10

View file

@ -4,7 +4,19 @@ from django.views.generic.detail import SingleObjectMixin
from .models import Election, Option, Question from .models import Election, Option, Question
class RestrictAccessMixin: class SelectElectionMixin:
"""Sélectionne automatiquement les foreignkeys voulues"""
def get_queryset(self):
qs = super().get_queryset()
if self.model is Question:
return qs.select_related("election")
elif self.model is Option:
return qs.select_related("question__election")
return qs
class RestrictAccessMixin(SelectElectionMixin):
"""Permet de restreindre l'accès à des élections/questions/options""" """Permet de restreindre l'accès à des élections/questions/options"""
f_prefixes = {Election: "", Question: "election__", Option: "question__election__"} f_prefixes = {Election: "", Question: "election__", Option: "question__election__"}