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 个比特币时,出现错误:

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

这没有按预期工作: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是一大堆比特币。

相关内容

最新更新