我有一個名爲checkout_items
Python字典的列表,通過簡單的函數創建(這裏進一步簡化,便於閱讀):Python字典參考返回一些奇怪
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'])
但名稱變量被設置爲這樣:<bound method CartItem.name of <CartItem: CartItem object>
然而,SKU(ALPH像「name」這樣的數字字符串)通過就好了。兩者都直接來自MySQL。任何想法發生了什麼?
顯示models.py和更多代碼,請 – lalo
您很可能有一個名爲'name'的模型方法,它可能會覆蓋mysql字段。你能顯示「CarItem」的型號代碼嗎? – karthikr