CruZer0
CruZer0

Reputation: 47

Django | Generate Total Price from Price per Item and Amount?

im new to Django and im playing a bit around. So i want to create a Model Field that is dynamicly showing the total price generated from the Amount and Item Price given by the User. Is there a way to do this easy? I would love to find out how that works.

Upvotes: 0

Views: 708

Answers (1)

Rone
Rone

Reputation: 166

I don't think you need a model field for that. You can create a method to do that for you. In your models.py, you can write a function like this inside the class of your model.

def total_price(self):
    return self.price * self.quantity

In your HTML templates, you can get the total price by doing something like:

<p>Price ${{item.total_price}}</p>

Upvotes: 1

Related Questions