@
@
@intermed2011-02-13 10:48:56
Django
@intermed, 2011-02-13 10:48:56

Django: How to pass a POST to a form for processing?

Hello. I have this form:

class LocalityForm(forms.Form):<br>
   region = forms.ModelChoiceField(queryset=Region.objects.all(), required=False, empty_label="")<br>
   locality = forms.ModelChoiceField(queryset=Locality.objects.none(), required=False, empty_label="")<br><br>
   #для того чтоб можно было выбрать населенный пункт в зависимости от<br>
   #региона переданного в POST, а так же вернуть введенные уже данные дописал к форме так<br>
   def __init__(self, data = None, *args, **kwargs):<br>
       super(forms.Form, self).__init__(*args, **kwargs)<br>
       if data:<br>
         if data.get('region'):<br>
           self.reg = data['region']<br>
           self.fields['region'].initial = self.reg<br>
           self.fields['locality'].queryset = Locality.objects.filter(region=self.reg).order_by('name')<br>
         if data.get('locality'):<br>
           self.loc = data['locality']<br>
           self.fields['locality'].initial = self.loc<br>

called quite simply form = LocalityForm(request.POST)
But then the form validation disappears, i.e. the form is obtained in any case not valid, apparently due to the interception of POST data by the data argument.
I don’t want to pass through named arguments - because the form call becomes rather idiotic
form = LocalityForm(request.POST, region = request.POST['region'], locality = request.POST['locality'])
Although if there is no other solution, then apparently you have to be content with what you have.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question