1👍
✅
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
Source:stackexchange.com