Python Django 2.0 十进制字段 "Ensure that there are no more than 3 digits before the decimal point"

Python Django 2.0 DecimalField "Ensure that there are no more than 3 digits before the decimal point"

我的金额列属性设置为 max_digits = 13,decimal_places = 7 因为你可以技术上有类似 10000.0000001 比特币的东西。

当我尝试在我的表格中输入并提交 0.1 比特币时,出现错误:

Ensure that there are no more than 3 digits before the decimal point.

这没有按预期工作:0.1 不超过 3 位,即使是我应该仍然可以设置超过 3 位。这里发生了什么?

models.py

class Transactions(models.Model):   
    user = models.ForeignKey(User, on_delete = models.CASCADE)  
    coin = models.CharField(max_length = 64)  
    buysell = models.CharField(default = 'buy', max_length = 4)
    amount = models.DecimalField(max_digits = 13, decimal_places = 7)  
    trade_price = models.DecimalField(max_digits = 5, decimal_places = 2)
    trade_date = models.DateTimeField(auto_now = True) 

forms.py

class TransactionForm(forms.ModelForm): 

    CHOICES = ((1, 'Buy'), (2, 'Sell'),)

    coin = forms.ModelChoiceField(queryset = Coin.objects.all()) 
    buysell = forms.ChoiceField(choices = CHOICES)

    field_order = ['buysell', 'coin', 'amount', 'trade_price']

    class Meta:
        model = Transactions
        fields = {'buysell', 'coin', 'amount', 'trade_price'}

如你所说,0.1小数点前不超过3位,所以应该不会报那个错。因此,错误可能来自不同的领域。

您没有说明哪个字段出错,或者您为其他字段提交了哪些值,但我怀疑问题出在您的 trade_price 字段上。

trade_price = models.DecimalField(max_digits = 5, decimal_places = 2)

目前支持最大值999.99。因此,如果您输入 trade_price=10000,您将收到 no more than 3 digits 错误。

给出 trade_price 的十进制值,如下所示:

trade_price = "10000.0000001"

我相信你一直是这样做的:

trade_price = 10000.0000001

引号是区别。 PS 10000 是一吨比特币。