“不支持从方法到十进制的转换django”。错误 [英] "conversion from method to Decimal is not supported django" error
本文介绍了“不支持从方法到十进制的转换django”。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我正在尝试计算以下参数的咖啡价格。但是,每次我尝试调用coffeeprice方法时,都会出现不支持从方法到十进制的转换错误。
So, I'm trying to calculate the coffee price of the parameters mentioned below. But, every time I try to call the coffeeprice method, it gives me a conversion from method to Decimal is not supported
error.
我的视图和模型中分别包含以下代码:
I have the following code in my view and model respectively:
def order(request):
if not request.user.is_authenticated:
raise Http404()
order_form = OrderForm(request.POST or None)
if order_form.is_valid():
obj = order_form.save(commit=False)
obj.price = Decimal(obj.coffeeprice)
obj.save()
messages.success(request, "Thank you for your order.")
return redirect("arabica:list")
context = {
"order_form": order_form,
}
return render(request, 'order.html', context)
class Coffee(models.Model):
ONE = 1
TWO = 2
THREE = 3
FOUR = 4
FIVE = 5
SHOTS_CHOICES = (
(ONE, 'One'),
(TWO, 'Two'),
(THREE, 'Three'),
(FOUR, 'Four'),
(FIVE, 'Five'),
)
user = models.ForeignKey(User, default=1)
name = models.CharField(max_length=50)
bean_type = models.ForeignKey(CoffeeBean)
roast_type = models.ForeignKey(Roast)
shots_number = models.IntegerField(default=ONE, choices=SHOTS_CHOICES)
syrup_type = models.ManyToManyField(Syrup)
powder_type = models.ManyToManyField(Powder)
water = models.FloatField()
milk = models.BooleanField(default=False)
foam = models.FloatField()
extra_instructions = models.TextField()
price = models.DecimalField(max_digits=6, decimal_places=3)
completed = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
def coffeeprice(self):
total = 0
total += self.bean_type.price
total += self.roast_type.price
for syrup in self.syrup_type.all():
total += syrup.price
for powder in self.powder_type.all():
total += powder.price
if self.milk:
milk_price = .25
total += milk_price
shots_price = self.shots_number * .5
total += shots_price
return total
我尝试导入和放置 Decimal()
处于不同阶段和不同变量,没有运气。
I tried importing and placing Decimal()
at different stages and at different variables, with no luck.
非常感谢您的帮助。
谢谢。
推荐答案
您需要调用 obj.coffeeprice()
。
这篇关于“不支持从方法到十进制的转换django”。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文