英文:
Can't I set the number with a decimal part to "MinMoneyValidator()" and "MaxMoneyValidator()" in "MoneyField()" with Django-money?
问题
我使用Django-money,然后在MoneyField()
中将0.00
和999.99
分别设置为MinMoneyValidator()
和MaxMoneyValidator()
,如下所示:
# "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=[ # 这里 # 这里
MinMoneyValidator(0.00), MaxMoneyValidator(999.99),
]
)
然后,我尝试添加一个产品,如下所示:
但是,我收到以下错误:
TypeError: 'float'对象不可订阅
因此,我将0
和999
分别设置为MinMoneyValidator()
和MaxMoneyValidator()
,如下所示,然后上面的错误得到解决:
# "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=[ # 这里 # 这里
MinMoneyValidator(0), MaxMoneyValidator(999),
]
)
实际上,我可以在models.DecimalField()中将0.00
和999.99
分别设置为MinValueValidator()和MaxValueValidator(),而不会出现任何错误,如下所示:
# "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( # 这里
max_digits=5, decimal_places=2, default=0,
validators=[ # 这里 # 这里
MinValueValidator(0.00), MaxValueValidator(999.99)
],
)
那么,在MoneyField()
中不能设置带小数部分的数字到MinMoneyValidator()
和MaxMoneyValidator()
吗?
英文:
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()
?
答案1
得分: 1
For this, you can use Decimal():
from decimal import Decimal
class Product(models.Model):
# ...
price = MoneyField(
max_digits=5, decimal_places=2, default=0, default_currency='USD',
validators=[ # Here # Here
MinMoneyValidator(Decimal(0.00)), MaxMoneyValidator(Decimal(999.99)),
]
)
英文:
For this, you can use Decimal():
from decimal import Decimal
class Product(models.Model):
# ...
price = MoneyField(
max_digits=5, decimal_places=2, default=0, default_currency='USD',
validators=[ # Here # Here
MinMoneyValidator(Decimal(0.00)), MaxMoneyValidator(Decimal(999.99)),
]
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论