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

问题描述 投票:3回答:1

我的金额列属性设置为max_digits = 13,decimal_places = 7,因为从技术上讲,你可以使用10000.0000001比特币。

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

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

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

models.朋友

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.朋友

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'}
python django django-models django-2.0
1个回答
3
投票

正如你所说,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错误。

© www.soinside.com 2019 - 2024. All rights reserved.