好的,所以我将从我的代码开始:
模型.py
class Order(models.Model):
order_id = models.IntegerField(primary_key = True)
order_status = models.IntegerField()
orderItems = []
视图.py
def addToList(request):
prodId = request.GET['prodId'] or None
if not prodId is None:
prod = Product.objects.get(prod_id=prodId)
custOrder = request.session.get(definitions.CUSTOMER_ORDER) or \
Order(order_total = 0, order_status=1)
custOrder.addOrderItem(prod)
request.session[definitions.CUSTOMER_ORDER] = custOrder
return HttpResponseRedirect('/')
def home(request):
shoppingList = request.session.get(definitions.CUSTOMER_ORDER)
好的,如您所见,我创建了订单模型,然后将 orderItem 添加到该模型中的列表并将其存储在会话中。现在这工作正常,数据按预期持续存在。事情是当我重新启动服务器时, Order 模型仍然在我想要的会话中,但列表对象是EMPTY。orderItems = [] 当我调试它时。
现在我不是 python 大师,所以如果有人能指出我做错了什么,我将不胜感激。
编辑:为 models.py 添加 orderItem 模型
class OrderItem(models.Model):
orderItem_id = models.IntegerField(primary_key = True)
orderItem_cost = models.FloatField()
orderItem_quantity = models.IntegerField()
order = models.ForeignKey(Order)
def getOrderCost(self):
return self.orderItem_cost * self.orderItem_quantity
class Meta:
db_table = u'orderItems'