I have a problem with dropdown dependent. in the frontend it works perfectly and filters me optimally but in the backend I have problems saving.
Choose a valid option. The choice made does not appear among those available.
This error is due to the fact that in form.py I cannot find 'gruppo_single' in self.data because it was excluded from the form to be dynamically passed from the view during the post.
Can anyone help me with this problem?
form for creating exercises (with ajax implementation) and groups
class EserciziForm(forms.ModelForm):
class Meta:
model = models.DatiEsercizi
exclude = ['gruppo_single']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['dati_esercizio'].queryset = models.Esercizi.objects.none()
if 'gruppo_single' in self.data:
gruppo_id = int(self.data.get('gruppo'))
self.fields['dati_esercizio'].queryset = models.Esercizi.objects.filter(gruppo_id = gruppo_id)
else:
print("-----errore------")
class GruppiForm(forms.ModelForm):
class Meta:
model = models.DatiGruppi
exclude = ['gruppi_scheda']
CodePudding user response:
this is the solution.
class EserciziForm(forms.ModelForm):
class Meta:
model = models.DatiEsercizi
exclude = ['gruppo_single']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['dati_esercizio'].queryset = models.Esercizi.objects.none()
if 'gruppo-dati_gruppo' in self.data:
gruppo_id = int(self.data.get('gruppo-dati_gruppo'))
self.fields['dati_esercizio'].queryset = models.Esercizi.objects.filter(gruppo_id = gruppo_id)