我正在使用Python和Django开发Web应用程序。创建新对象时出现问题。我创建了对象,并将数据存储在db中,并将响应返回给客户端,但是当我尝试创建对象时,下一个请求发现一些变量已经定义为上一个请求中的值。>
代码如下:
cart_object = json.loads(request.POST.get("cart"))
cart: CartModel = CartModel()
cart.cart_id = IntHelper.string_to_int(cart_object.get("cart_id"), None)
cart.entity_id = IntHelper.string_to_int(cart_object.get("entity_id"), None)
cart.user_id = IntHelper.string_to_int(cart_object.get("user_id"), None)
# cart.cart_item = []
list_items = cart_object.get("cart_item")
for i in range(len(list_items)):
cart_item: CartItemModel = CartItemModel()
cart_item.cart_id = IntHelper.string_to_int(cart.cart_id, None)
cart_item.user_id = IntHelper.string_to_int(cart.user_id, None)
cart_item.category_id = IntHelper.string_to_int(list_items[i].get("category_id"), None)
cart_item.quantity = FloatHelper.string_to_float(list_items[i].get("quantity"), None)
cart_item.seller_bid_price = FloatHelper.string_to_float(list_items[i].get("seller_bid_price"), None)
cart_item.seller_gst_percentage = FloatHelper.string_to_float(list_items[i].get("seller_gst_percentage"),
None)
cart_item.files = list_items[i].get("files")
cart.cart_item.append(cart_item)
cart_service: CartService = CartService()
cart_object = cart_service.add(cart)
return self.send_response(cart_object)
每次创建CartModel对象时,它都会列出CartItemModel。
在第二次调用此方法后,创建了CartModel对象。所有其他字段为空,并且CartItemModel列表的值与先前创建的对象中的一样
class CartModel:
def __init__(self, cart_id=None, entity_id=None, user_id=None, cart_date=None, status=None, cart_item=[]):
self.cart_id = cart_id
self.entity_id = entity_id
self.user_id = user_id
self.cart_date = cart_date
self.status = status
self.cart_item = cart_item
请帮助我解决此问题。不知道哪里出了问题。