كيف أصير أكتب مثل هذا كود
def cart_update(request, pid):
if not request.session.session_key:
request.session.create()
session_id = request.session.session_key
cart_model = Cart.objects.filter(session=session_id).last()
if cart_model is None:
cart_model = Cart.objects.create(session_id=session_id, items=[pid])
elif pid not in cart_model.items:
cart_model.items.append(pid)
cart_model.save()
return JsonResponse({
'message': _('The product has been added to your cart.'),
'items_count': len(cart_model.items)
})