Python Django 2.0 DecimalField“确保小数点前不超过3位数”

Ski*_*345 3 python django django-models django-2.0

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

当我尝试在表单上输入并提交 0.1 个比特币时,出现错误:

确保小数点前不超过 3 位数字。

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

模型.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) 
Run Code Online (Sandbox Code Playgroud)

表格.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'}
Run Code Online (Sandbox Code Playgroud)

Ala*_*air 7

如您所说,0.1小数点前不超过 3 位,因此不应出现该错误。因此,错误可能来自不同的领域。

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

trade_price = models.DecimalField(max_digits = 5, decimal_places = 2)
Run Code Online (Sandbox Code Playgroud)

目前,这支持的最大值为999.99。因此,如果您输入trade_price=10000,则会no more than 3 digits出现错误。