Django session(serialization error)?

Good day! Tell me is it possible in session to store multiple variables? this is the case, I store in a session the cart of the store, and wanted there to be wish_list(so the models don't produce) to settings.py prescribed two variables
CART_SESSION_ID = 'cart'
WISH_SESSION_ID = 'wish_list'

everything seems to be fine when initialize created 2 empty dictionary with keys respectively project works when 2 of the key blank or both filled in but when one of the keys is empty then crash with an error Decimal('400.00') is not JSON serializable though it probably has no direct relationship because when saponetta the second key there is no error!
maybe you have any thoughts?

from decimal import Decimal
from django.conf import settings
from shop.models import Product


class Cart(object):
 def __init__(self, request):
 # Initial cart for users
 self.session = request.session
 cart = self.session.get(settings.CART_SESSION_ID)
 if not cart:
 # Save cart user in to session
 cart = self.session[settings.CART_SESSION_ID] = {}
 self.cart = cart

 def add(self, product, quantity=1):
 product_id = str(product.id)
 if product_id not in self.cart:
 self.cart[product_id] = {'quantity': 1, 'price': str(product.price)}
 elif quantity == ":
 quantity = 1
 self.cart[product_id]['quantity'] = quantity
else:
 self.cart[product_id]['quantity'] = int(quantity)
self.save()

 # save date in session
 def save(self):
 self.session[settings.CART_SESSION_ID] = self.cart
 # in-session modified
 self.session.modified = True

 # delete product in cart
 def remove(self, product):
 product_id = str(product.id)
 if product_id in self.cart:
 del self.cart[product_id]
self.save()

 # iteration for product
 def __iter__(self):
 product_ids = self.cart.keys()
 products = Product.objects.filter(id__in=product_ids)
 for product in products:
 self.cart[str(product.id)]['product'] = product

 for item in self.cart.values():
 item['price'] = Decimal(item['price'])
 item['quantity'] = int(item['quantity'])
 item['total_price'] = item['price'] * item['quantity']
 yield item

 def __len__(self):
 return sum(int(item['quantity']) for item in self.cart.values())

 def get_total_price(self):
 return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())

 def clear(self):
 del self.session[settings.CART_SESSION_ID]
 self.session.modified = True

actually basket
class WishList(object):
 def __init__(self, request):
 # Initial wish_list for users
 self.session = request.session
 wish_list = self.session.get(settings.WISH_SESSION_ID)
 if not wish_list:
 # Save wish_list user in to session
 wish_list = self.session[settings.WISH_SESSION_ID] = {}
 self.wish_list = wish_list

 def add(self, product):
 product_id = str(product.id)
 if product_id not in self.wish_list:
 self.wish_list[product_id] = {'price': str(product.price)}
self.save()

 # save date in session
 def save(self):
 self.session[settings.WISH_SESSION_ID] = self.wish_list
 # in-session modified
 self.session.modified = True

 # delete product in cart
 def remove(self, product):
 product_id = str(product.id)
 if product_id in self.wish_list:
 del self.wish_list[product_id]
self.save()

 def __iter__(self):
 product_ids = self.wish_list.keys()
 products = Product.objects.filter(id__in=product_ids)
 for product in products:
 self.wish_list[str(product.id)]['product'] = product

 for item in self.wish_list.values():
 item['price'] = Decimal(item['price'])
 yield item

 def __len__(self):
 return len(self.wish_list)

and wish list
def wish_list_add(request, product_id):
 wish_list = WishList(request)
 product = get_object_or_404(Product, id=product_id)
wish_list.add(product=product)
 return redirect('cart:wish_list_detail')

the basket is approximately the same
July 2nd 19 at 18:13
0 answer

Find more questions by tags DjangoPython