简体   繁体   中英

Ensure that there are no more than 3 digits before the decimal point error when submitting a form

I am getting the error "Ensure that there are no more than 3 digits before the decimal point" when I am trying to submit a modal form.

How can I fix it?

my model:

class Product(models.Model):
    quantity = models.DecimalField( "Quantity", max_digits=8, decimal_places=2, default=0)
    retail_price = models.DecimalField( max_digits=8, decimal_places=4, default=0, blank=True)
    purchase_price= models.DecimalField( max_digits=8, decimal_places=2, default=0, blank=True)
    timi_xondrikis = models.DecimalField( max_digits=8, decimal_places=2, default=0, blank=True)

my form:

class ProductForm(ModelForm):
    class Meta:
        model = Product
        #fields = '__all__'
        fields=('quantity','retail_price')

        widgets = {
    'retail_price' : forms.NumberInput(attrs={'max':9999.99})
}

Giving to price field 900 or 900.00 is acceptable but giving 1000 or 1000.00 which I want, is not. The thing is that i define my retail_price field to have max_digits=10.

在此处输入图片说明

Why this happening?

Basically you need to increase decimal_places , as per documentation :

For example, to store numbers up to 999 with a resolution of 2 decimal places, you'd use:

 models.DecimalField(..., max_digits=5, decimal_places=2) 

So use decimal_places=4 if you want to store 10000.

Add a widget for your retail_price in the form Meta which specifies the max value

widgets = {
    'retail_price' : forms.NumberInput(attrs={'max':999.99})
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM