我有一个名为 的python 字典列表checkout_items
,由一个简单的函数创建(为了便于阅读,此处进一步简化):
def checkout_items(request):
items = get_cart_items(request)
co_items = [] #a list of dictionaries to be used by the cart
#iterate through the items and homogenize them into a standardized checkout_item format
for i in items:
co_item = {'number': num,
'name': i.name,
'sku': i.sku,
'quantity': i.quantity,
'price': i.price}
我在视图的其他地方引用了这个列表(再次简化):
checkout_items = cart.checkout_items(request)
attributes = {}
for i in checkout_items:
attributes['item_name_'+ str(i['number'])] = str(i['name'])
attributes['item_number_'+ str(i['number'])] = str(i['sku'])
attributes['quantity_'+ str(i['number'])] = str(i['quantity'])
但是 name 变量被设置为:<bound method CartItem.name of <CartItem: CartItem object>
然而,sku(字母数字字符串,就像“名称”一样)似乎很好。两者都直接来自 MySQL。有什么想法吗?