2

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

I tried importing and placing Decimal() at different stages and at different variables, with no luck.

Your help is greatly appreciated.

Thank you.

wencakisa
  • 5,850
  • 2
  • 15
  • 36
Yousif G
  • 23
  • 1
  • 4

1 Answers1

3

You need to call obj.coffeeprice().

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I did under `if order_form.is_valid():` – Yousif G Aug 19 '17 at 12:40
  • @YousifG, he means that you need to call the method `coffeeprice()` on this line: `obj.price = Decimal(obj.coffeeprice())`, because you want to get the return value of the method to be transformed to `Decimal`, not the method itself. That's why you receive the error `Conversion from method to Decimal is not supported`. – wencakisa Aug 19 '17 at 12:42
  • No you didn't. You need to *call* it. – Daniel Roseman Aug 19 '17 at 12:42
  • Excuse my lack of knowledge, but can you explain where exactly to place the method? – Yousif G Aug 19 '17 at 12:44
  • I don't understand what you're asking. Everything is where it is supposed to be. You just need to use the `()` syntax to call the method rather than simply referring to it. – Daniel Roseman Aug 19 '17 at 12:45
  • @YousifG See this: https://stackoverflow.com/questions/19130958/what-does-it-mean-to-call-a-function-in-python – wencakisa Aug 19 '17 at 12:46
  • It worked, it was just missing the parenthesis! Such a rookie mistake. Thank you very much! – Yousif G Aug 19 '17 at 12:47