Allow only positive decimal numbers
Within my Django models I have created a decimal field like this:
price = models.DecimalField(_(u'Price'), decimal_places=2, max_digits=12)
Obviously it makes no sense for the price to be negative or zero. Is there a way to limit the decimal number to only positive numbers?
Or do I have to capture this using form validation?
Use the MinValueValidator
.
price = models.DecimalField(_(u'Price'), decimal_places=2, max_digits=12, validators=[MinValueValidator(Decimal('0.01'))])
You could do something like this :
# .....
class priceForm(ModelForm):
price = forms.DecimalField(required=False, max_digits=6, min_value=0)
This, also, is responsible for the validator value of 'price'.