I use Django-money, then I set 0.00
and 999.99
to MinMoneyValidator()
and MaxMoneyValidator()
respectively in MoneyField()
as shown below:
# "models.py"
from django.db import models
from djmoney.models.fields import MoneyField
from djmoney.models.validators import MinMoneyValidator, MaxMoneyValidator
class Product(models.Model):
name = models.CharField(max_length=50)
price = MoneyField( # Here
max_digits=5, decimal_places=2, default=0, default_currency='USD',
validators=[ # Here # Here
MinMoneyValidator(0.00), MaxMoneyValidator(999.99),
]
)
Then, I tried to add a product as shown below:
But, I got the error below:
TypeError: 'float' object is not subscriptable
So, I set 0
and 999
to MinMoneyValidator()
and MaxMoneyValidator()
respectively in MoneyField()
as shown below, then the error above was solved:
# "models.py"
from django.db import models
from djmoney.models.fields import MoneyField
from djmoney.models.validators import MinMoneyValidator, MaxMoneyValidator
class Product(models.Model):
name = models.CharField(max_length=50)
price = MoneyField(
max_digits=5, decimal_places=2, default=0, default_currency='USD',
validators=[ # Here # Here
MinMoneyValidator(0), MaxMoneyValidator(999),
]
)
Actually, I can set 0.00
and 999.99
to MinValueValidator() and MaxValueValidator() respectively in models.DecimalField() without any errors as shown below:
# "models.py"
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
class Product(models.Model):
name = models.CharField(max_length=50)
price = models.DecimalField( # Here
max_digits=5, decimal_places=2, default=0,
validators=[ # Here # Here
MinValueValidator(0.00), MaxValueValidator(999.99)
],
)
So, can't I set the number with the decimal part to MinMoneyValidator()
and MaxMoneyValidator()
in MoneyField()
?
For this, you can use Decimal():