django: How to pass in the form POST processing

0 like 0 dislike
5 views
Hello. I have this form:

class LocalityForm(forms.Form): region = forms.ModelChoiceField(queryset=Region.objects.all(), required=False, empty_label=") locality = forms.ModelChoiceField(queryset=Locality.objects.none(), required=False, empty_label=") #in order to be able to select the city depending on #region transferred to the POST, and back in already entered data added to the form so def __init__(self, data = None, *args, **kwargs): super(forms.Form, self).__init__(*args, **kwargs) if data: if data.get('region'): self.reg = data['region'] self.fields['region'].initial = self.reg self.fields['locality'].queryset = Locality.objects.filter(region=self.reg).order_by('name') if data.get('locality'): self.loc = data['locality'] self.fields['locality'].initial = self.loc 


called quite simply form = LocalityForm(request.POST)
But then disappears test the form, i.e. the form is in any case not valid, probably due to the intercept POST data argument data.

To pass through named arguments is not desirable because the press is becoming quite idiotic
form = LocalityForm(request.POST, region = request.POST['region'], locality = request.POST['locality'])
Although if there is no other solution that apparently will have to settle for what we have.
by | 5 views

1 Answer

0 like 0 dislike
in General, change the form depending on the received data it is a strange desire, *select area depending on the region* you will not succeed, because initially the form will render empty lists of settlements. it seems to me to add ajax view and do the same JavaScript om is a viable solution.
\r
to overcome your problem you can just pass the data in __init__
\r
super(forms.Form, self).__init__(data, *args, **kwargs)
by

Related questions

0 like 0 dislike
1 answer
asked Apr 11, 2019 by GhostFantik
0 like 0 dislike
4 answers
0 like 0 dislike
2 answers
asked Apr 1, 2019 by RokkerRuslan
0 like 0 dislike
4 answers
0 like 0 dislike
2 answers
asked Apr 1, 2019 by RokkerRuslan
110,608 questions
257,186 answers
0 comments
32,849 users