Can't I set the number with a decimal part to "MinMoneyValidator()" and "MaxMoneyValidator()" in "MoneyField()" with Django-money?

huangapple go评论51阅读模式
英文:

Can't I set the number with a decimal part to "MinMoneyValidator()" and "MaxMoneyValidator()" in "MoneyField()" with Django-money?

问题

我使用Django-money,然后在MoneyField()中将0.00999.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),
        ]
    )

然后,我尝试添加一个产品,如下所示:

Can't I set the number with a decimal part to "MinMoneyValidator()" and "MaxMoneyValidator()" in "MoneyField()" with Django-money?

但是,我收到以下错误:

TypeError: 'float'对象不可订阅

因此,我将0999分别设置为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.00999.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:

Can't I set the number with a decimal part to "MinMoneyValidator()" and "MaxMoneyValidator()" in "MoneyField()" with Django-money?

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)),
        ]
    )

huangapple
  • 本文由 发表于 2023年2月6日 17:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定