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) -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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.