Python字典引用返回了一些奇怪的东西
我有一个名为Python字典引用返回了一些奇怪的东西,python,django,list,dictionary,Python,Django,List,Dictionary,我有一个名为checkout\u items的python字典列表,由一个简单的函数创建(此处进一步简化,以便于阅读): 我在视图的其他地方引用了此列表(同样,简化): 但是name变量被设置为:正如您所说,您提供给函数checkout\u items()的数据来自get\u cart\u items() 因此,items不是一个愚蠢的类似名称空间的对象,而是一个具有您编写的方法的对象,所有这些方法都是CartItems 然后,在构建co_项时,将i.name提供给字典,该字典随后将打印为绑定方
checkout\u items
的python字典列表,由一个简单的函数创建(此处进一步简化,以便于阅读):
我在视图的其他地方引用了此列表(同样,简化):
但是name变量被设置为:
正如您所说,您提供给函数checkout\u items()
的数据来自get\u cart\u items()
因此,items
不是一个愚蠢的类似名称空间的对象,而是一个具有您编写的方法的对象,所有这些方法都是CartItem
s
然后,在构建co_项时,将i.name
提供给字典,该字典随后将打印为绑定方法CartItem.name of
看起来您的CartItem
对象有一个方法名!你应该试试i.name()
。或者,如果name()
是其他属性,则使用任何真正具有名称的属性。正如您所说,您提供给函数checkout\u items()
的数据来自get\u cart\u items()
因此,items
不是一个愚蠢的类似名称空间的对象,而是一个具有您编写的方法的对象,所有这些方法都是CartItem
s
然后,在构建co_项时,将i.name
提供给字典,该字典随后将打印为绑定方法CartItem.name of
看起来您的CartItem
对象有一个方法名!你应该试试i.name()
。或者,如果name()
是其他属性,则使用真正具有名称的任何属性。显示models.py和更多代码,请使用名为name
的模型方法,该方法可能会覆盖mysql字段。能否显示CarItem
的模型代码?请显示models.py和更多代码,您很可能有一个名为name
的模型方法,该方法可能会覆盖mysql字段。你能展示一下CarItem的模型代码吗?你和zmo都是对的,我愚蠢地没有正确地调用这个方法。对于我为什么这样做,我有一个勉强可以接受的借口,但我的代码一开始就不应该允许这种误解。不管怎样,你先回答,所以我接受了你的回答。谢谢你和zmo都是对的,我愚蠢地未能正确调用该方法。对于我为什么这样做,我有一个勉强可以接受的借口,但我的代码一开始就不应该允许这种误解。不管怎样,你先回答,所以我接受了你的回答。谢谢
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>