首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"conversion from method to Decimal is not supported“错误

"conversion from method to Decimal is not supported“错误
EN

Stack Overflow用户
提问于 2017-08-19 20:34:06
回答 1查看 2.5K关注 0票数 1

因此,我正在尝试计算下面提到的参数的咖啡价格。但是,每次我尝试调用咖啡价格方法时,它都会给我一个conversion from method to Decimal is not supported错误。

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

代码语言:javascript
复制
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(),但没有成功。

非常感谢您的帮助。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-19 20:36:21

你需要给obj.coffeeprice()打电话。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45771590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档