"不支持从方法到十进制的转换django"错误

时间:2017-08-19 12:34:06

标签: python django

所以,我试图计算下面提到的参数的咖啡价格。但是,每当我尝试调用coffeeprice方法时,它都会给我conversion from method to Decimal is not supported错误。

我的视图和模型中分别有以下代码:

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(),但没有运气。

非常感谢您的帮助。

谢谢。

1 个答案:

答案 0 :(得分:1)

您需要致电obj.coffeeprice()