我的金额列属性设置为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'}
正如你所说,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
错误。