我的一个表单中有一个Decimalfield,用户可以在其中输入价格.
假设用户输入11.00,现在当我检索它时(priceform.leaned_data),它返回
Decimal('11.00')
所以看起来像是
price = Decimal('11.00')
这会在我try 将其插入数据库时触发验证错误.
我想不出任何解决这个问题的办法
models.py个
class Bid(models.Model):
title = models.CharField(max_length=64, blank=True)
date_time = models.DateTimeField(default=timezone.now, blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
user = models.CharField(max_length=64)
forms.py:个
class BidForm(ModelForm):
class Meta:
model = Bid
fields = ['price']
views.py:个
if request.method == "POST":
bidform = BidForm(request.POST)
if bidform.is_valid():
price = bidform.cleaned_data['price']
bid = Bid.objects.create(title=title, price=bidform, user=username)
bid.save()