100👍
Use the MinValueValidator
.
price = models.DecimalField(_(u'Price'), decimal_places=2, max_digits=12, validators=[MinValueValidator(Decimal('0.01'))])
4👍
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’.
- [Django]-Django south migration – Adding FULLTEXT indexes
- [Django]-Reducing Django Memory Usage. Low hanging fruit?
- [Django]-Is it possible to generate django models from the database?
3👍
In Django 2.2 you can add constraints to a model which will be applied in the migrations as a constraint on the database table:
from decimal import Decimal
from django.db import models
class Item(models.Model):
price = models.DecimalField( _(u'Price'), decimal_places=2, max_digits=12 )
class Meta:
constraints = [
models.CheckConstraint(check=models.Q(price__gt=Decimal('0')), name='price_gt_0'),
]
Validation of Constraints
In general constraints are not checked during
full_clean()
, and do not raiseValidationErrors
. Rather you’ll get a database integrity error onsave()
.
- [Django]-Django: Redirect logged in users from login page
- [Django]-Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?
- [Django]-Django Aggregation – Expression contains mixed types. You must set output_field
1👍
Assuming this is your product model, and you want to add non-negative constraint on price field. You can add the meta constraint on the model:
class Product(models.Model):
price = models.DecimalField(max_digits=13, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
managed = True
db_table = 'product'
constraints = [
models.CheckConstraint(check=models.Q(price__gte='0'), name='product_price_non_negative'),
]
- [Django]-Loading initial data with Django 1.7+ and data migrations
- [Django]-Using JSON in django template
- [Django]-Creating my own context processor in django
1👍
- Import dependencies
from decimal import Decimal
from django.core.validators import MinValueValidator
- Add validators
price = models.DecimalField(
decimal_places=2,
max_digits=12,
validators=[MinValueValidator(Decimal('0.01'))]
)
- [Django]-Django – why is the request.POST object immutable?
- [Django]-WSGI vs uWSGi with Nginx
- [Django]-Add rich text format functionality to django TextField
0👍
According to the docs, it seems that there’s no way to put something like a database constraint on a field. The best you can do is add model “validators” which will be called if you call model validation or use a ModelForm
. The validators are skipped if you simply put values into an object and save()
.
So, you can add validation on the forms or you can add validation to the model which also get run on the form level if you use ModelForm
.
From the docs on “How validators are run”:
See the form validation for more information on how validators are run
in forms, and Validating objects for how they’re run in models. Note
that validators will not be run automatically when you save a model,
but if you are using aModelForm
, it will run your validators on any
fields that are included in your form. See the ModelForm documentation
for information on how model validation interacts with forms.
- [Django]-Get count of related model efficiently in Django
- [Django]-Django Rest Framework remove csrf
- [Django]-Can Django automatically create a related one-to-one model?