mirror of
https://github.com/ae-utbm/sith.git
synced 2025-09-13 11:35:44 +00:00
fix n+1 issue with InvoiceCall
This commit is contained in:
@@ -6,7 +6,7 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
from phonenumber_field.widgets import RegionalPhoneNumberWidget
|
from phonenumber_field.widgets import RegionalPhoneNumberWidget
|
||||||
|
|
||||||
from club.widgets.ajax_select import AutoCompleteSelectClub
|
from club.widgets.ajax_select import AutoCompleteSelectClub
|
||||||
from core.models import User
|
from core.models import User, Club
|
||||||
from core.views.forms import NFCTextInput, SelectDate, SelectDateTime
|
from core.views.forms import NFCTextInput, SelectDate, SelectDateTime
|
||||||
from core.views.widgets.ajax_select import (
|
from core.views.widgets.ajax_select import (
|
||||||
AutoCompleteSelect,
|
AutoCompleteSelect,
|
||||||
@@ -377,22 +377,24 @@ BasketForm = forms.formset_factory(
|
|||||||
|
|
||||||
|
|
||||||
class InvoiceCallForm(forms.Form):
|
class InvoiceCallForm(forms.Form):
|
||||||
def __init__(self, *args, month=None, clubs=None, **kwargs):
|
def __init__(self, *args, month=None, clubs: list[Club] | None = None, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.month = month
|
self.month = month
|
||||||
self.clubs = clubs
|
self.clubs = clubs
|
||||||
|
|
||||||
for club in self.clubs:
|
if self.clubs is None:
|
||||||
field_name = f"club_{club.id}"
|
self.clubs = []
|
||||||
initial = (
|
invoices = {
|
||||||
InvoiceCall.objects.filter(club=club, month=month)
|
i["club_id"]: i["is_validated"]
|
||||||
.values_list("is_validated", flat=True)
|
for i in InvoiceCall.objects.filter(club__in=self.clubs).values(
|
||||||
.first()
|
"club_id", "is_validated"
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
for club in self.clubs:
|
||||||
|
is_validated = invoices.get(club.id, False)
|
||||||
|
|
||||||
self.fields[field_name] = forms.BooleanField(
|
self.fields[f"club_{club.id}"] = forms.BooleanField(
|
||||||
required=False,
|
required=False, initial=is_validated
|
||||||
initial=initial,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
Reference in New Issue
Block a user