I have a form that lets me first select a product type and then select the product. As i have 1000+ products i use the following to filter the product list to improve performance.
I have the following inlineform in my views.py
OrderLineFormSet = inlineformset_factory(OrderHeader, OrderLine, OrderLineForm, extra = 1)
In my forms.py i check if there is already a product selected. If there is a product selected i only show the products with the same product type to improve load performance. If a product is empty it will load all product options so it will let me save the form after selection.
class OrderLineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(OrderLineForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_show_errors = True
self.helper.error_text_inline = False
if self.instance.product is not None:
self.fields['product'] = forms.ModelChoiceField(queryset=Product.objects.filter(product_type_id=self.instance.product_type_id), required=False)
This results in the following form
However, when i change the Product type on an existing form (and then use jQuery to update the Product dropdown) i get an error saving. I know this is because the selection is not an option in the dropdown.
My question: How can i disable this error so it saves the option i selected, regardless of the original options.
Below you will find my views.py for this form
def orderline_formset(request, id=None):
OrderLineFormSet = inlineformset_factory(OrderHeader, OrderLine, OrderLineForm, extra = 1)
orderheader = None
orderid = None
orderheaderid = 0
if id:
orderid = OrderHeader.objects.get(pk=id)
if request.POST:
if orderid:
form = OrderHeaderForm(request.POST, instance=orderid)
formset = OrderLineFormSet(request.POST,instance=orderid)
else:
form = OrderHeaderForm(request.POST)
formset = OrderLineFormSet(request.POST)
if form.is_valid() and formset.is_valid():
if orderid:
form.save() # update object
else:
orderid = form.save() # create object
formset.instance = orderid
formset.save()
messages.success(request, 'Order saved succesfully!')
return HttpResponseRedirect('/orderline_formset/' + str(orderid.pk))
else: # form invalid
messages.error(request, 'Order save error, please check mandatory fields')
else: # request.GET
if orderid:
invoiceheader = ""
if orderid.orderheader_invoice:
invoiceheader = " -- Invoice " + str(orderid.orderheader_invoice)
orderheader = "Order " + str(orderid.pk) + invoiceheader
orderheaderid = orderid.pk
form = OrderHeaderForm(instance=orderid)
formset = OrderLineFormSet(instance=orderid)
else:
orderheader = "New Order"
orderheaderid = 0
form = OrderHeaderForm(instance=OrderHeader())
formset = OrderLineFormSet(instance=OrderHeader())
return render_to_response("order-add.html", {'form' : form,'formset': formset,
'orderheader': orderheader,
'orderheaderid': orderheaderid},
context_instance=RequestContext(request))
Override ModelChoiceField
, for example:
class MyModelChoiceField(ModelChoiceField):
def to_python(self, value):
try:
value = super(MyModelChoiceField, self).to_python(value)
except self.queryset.model.DoesNotExist:
key = self.to_field_name or 'pk'
value = Product.objects.filter(**{key: value})
if not value.exists():
raise ValidationError(self.error_messages['invalid_choice'], code='invalid_choice')
else:
value= value.first()
return value
And use it in your form.
self.fields['product'] = MyModelChoiceField(queryset=Product.objects.filter(product_type_id=self.instance.product_type_id), required=False)