1

I have HTML form with one optional field like this

<input type="number" name="total_amount" id="total_amount" class="completedtype" onchange="add()"/>

When I input some number in the field then I the whole form data gets saved in to the database. But when there is no data then I get Validation Error

ValidationError at /oms_data/ [u"'' value must be a decimal number."]

I have tried to resolve this issue for more than half a day but nothing helped me. I have tried the following to resolve this error.

  1. I have used something like this which was suggested in some SO answer:

    total_amount = request.POST.get("total_amount",0) ## get total_amount or take default value 0
    
  2. That didn't work so I used the famous try: except: as shown below:

    try: 
       total_amount = request.POST["total_amount"]  
    except:             ## whatever the exception and not just Validation Error
       total_amount = 0
    

I don't know why none of this worked.The error occurs when trying to save the form.

save_Order_Selling_Pricing = models.Order_Selling_Pricing(order_id=order_id,vendor_name = vendor_name,total_amount =total_amount,
                                                              vendor_discount_percent=vendor_discount_percent,dg_percent = dg_percent,
                                                              vendor_discount_amount=vendor_discount_amount,vendor_percent = vendor_percent,
                                                              dg_discount_percent = dg_discount_percent,dg_discount_amount=dg_discount_amount,
                                                              final_selling_price = final_selling_price,order_selling_pricing_id=order_selling_pricing_id,
                                                              order_payment_mode = order_payment_mode,present_datetime=present_datetime)
                save_Order_Selling_Pricing.save()

Any help in this regard would be great! Thanks in advance! I'm using Django 1.8 python 2.7 if this helps.

UPDATE 1: I have my models defined as shown below:

total_amount = models.DecimalField(max_digits=10,decimal_places=2,blank = True,null = True,default = Decimal('0.00'))
  • have you defined the field as optional? i.e. `blank=True` in the model? – Pynchia Jul 04 '15 at 20:51
  • see if [this](http://stackoverflow.com/questions/23268736/django-how-to-make-form-fields-optional) helps – Pynchia Jul 04 '15 at 20:53
  • Yes, assuming this is a ModelForm, show us the model code, if it's a non-model Form, show us the form code. – CrazyCasta Jul 04 '15 at 20:55
  • @CrazyCasta Please see my edited code. –  Jul 04 '15 at 20:56
  • @Pynchia : I have defined it in models as updated –  Jul 04 '15 at 20:57
  • I am not quite sure I am following the conundrum here. Were you able to assign `total_amount` in the end or not? What is your question exactly? – Pynchia Jul 04 '15 at 21:03
  • @Pynchia : That field is optional. If I enter 100 in that field then it gets saved but if I leave that field empty and save then I get that validation error . –  Jul 04 '15 at 21:15
  • have you tried `save_Order_Selling_Pricing = models.Order_Selling_Pricing(**form.cleaned_data)` – Pynchia Jul 04 '15 at 21:19
  • 1
    have you changed your model without making+running a migration? Maybe something is out-of-synch – Pynchia Jul 04 '15 at 21:22

0 Answers0