I have a strange problem with creation of shopping cart I in Django 2.0. Sopping cart is and object inherited from request and stored in session:
class Cart(object):
def __init__(self, request, shop_u_id):
self.session_key = str(settings.CART_SESSION_ID)
self.session = request.session
cart = self.session.get(self.session_key)
if not cart:
cart = self.session[self.session_key] = {}
self.cart = cart
I have no problem with set and get methods, but strange problem appeared in total sum function:
def get_total_price(self):
return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())
I got an error:
Object of type 'Decimal' is not JSON serializable
But get_total_price nothing stores in session, it just for representation. (item['price'] stored as string)