Django FORM for India Postal Address

56 views
Skip to first unread message

Priyabrata Das

unread,
Nov 6, 2016, 2:28:07 AM11/6/16
to Django users
I have a Django ModelForm as below. Primary objective is to select area pincode based on selected "state". I don't seem to find a way how can I filter Pincode based on a dynamic queryset on the chosen state. form_valid() method in CreateView is not invoked until the entire form is submitted.



template


<form method='POST' action="">{% csrf_token %}
{{ form|crispy }}



view.py


class UserAddressCreateView(FormView):
    form_class = UserAddressForm
    template_name = "user_address_form.html"
    success_url = "/checkout/address/"
   
   

forms.py

class UserAddressForm(forms.ModelForm):
    class Meta:
        model = UserAddress
        fields = '__all__'

    state = forms.ModelChoiceField(
        widget=forms.Select,
        queryset=StateCode.objects.all(),
        )
   
    place = forms.ModelChoiceField(
        widget=forms.Select,
        queryset=PinCode.objects.none(),
        )

   
    def clean(self):
        cleaned_data = super(UserAddressForm,self).clean()
        state = cleaned_data.get("state")
        return cleaned_data

 
    def __init__(self,  *args, **kwargs):
        super(UserAddressForm, self).__init__(*args, **kwargs)
        self.fields["place"].queryset = PinCode.objects.filter(state="KARNATAKA")




models.py

class UserAddress(models.Model):
    user = models.ForeignKey(UserCheckout)
    type = models.CharField(max_length=20, choices=ADDRESS_TYPE)
    address = models.CharField(max_length=120, null=True,blank=True)
    mobile = models.CharField(max_length=120, null=True,blank=True)
    state = models.ForeignKey(StateCode,null=True,blank=True)
    place = models.ForeignKey(PinCode,null=True,blank=True)
   

class StateCode(models.Model):
    state = models.CharField(max_length=120, blank=True)
    code  = models.CharField(max_length=120, blank=True)
   

class PinCode(models.Model):
    place = models.CharField(max_length=120, blank=True)
    pincode = models.CharField(max_length=120, blank=True)
    officeType = models.CharField(max_length=120, blank=True)
    Deliverystatus = models.CharField(max_length=120, blank=True)
    divisionname = models.CharField(max_length=120, blank=True)
    regionname = models.CharField(max_length=120, blank=True)
    circle =  models.CharField(max_length=120, blank=True)
    Taluk = models.CharField(max_length=120, blank=True)
    district = models.CharField(max_length=120, blank=True)
    state =  models.CharField(max_length=120, blank=True)
    Telephone = models.CharField(max_length=120, blank=True)
    Related_Suboffice = models.CharField(max_length=120, blank=True)
    Related_Headoffice = models.CharField(max_length=120, blank=True)   

Andreas Kuhne

unread,
Nov 6, 2016, 2:48:39 AM11/6/16
to django...@googlegroups.com
Hi,

There is a plugin called django-smart-selects, that enables you to get information from the server based on information in a select field. You could try that? The form_valid() method and clean methods where you can VALIDATE such information is only called when posting the form (as you have found out). 

You will need some sort of ajax requests to make that work - which django-smart-selects can help you with.

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f0ec1785-b57e-42f8-9824-f46de34e395b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages