Reputation: 6824
I'm trying to create custom shopping cart functionality. Basket in chart need to have total_price field. This is my first django shop App, so question is what is the better way, based on your experience, to calculate basket total. To calculate it in the model save or to create get_total() function outside the model in view?
Upvotes: 0
Views: 1357
Reputation: 18523
Adding items to a shopping cart is a much more frequent action than getting the total. You can save a lot of SQL updates if you don't maintain a total field. Besides, typically when you need the total, you also need to list the items anyway, and computing the sum of prices in python is cheap.
But don't take my word for it. See how Oscar implements the cart here:
(read the code of add_product
and _get_total
).
https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/basket/abstract_models.py
Upvotes: 1