2

我一直在尝试向预先存在的字典键添加一个值。该值来自Car具有名为 的属性的 db.Model price

尝试了两个代码块(如下),但得到了 AttributeError: 'GqlQuery' object has no attribute 'price'。

items = {"BE1234":"2","BE1923":"3","BE2032":"1", etc}
------------------------------------------------------------
block 1)
for item in items:
  cars = db.GqlQuery('SELECT * FROM Car WHERE item=:1',item)
  items[item] = cars.price
------------------------------------------------------------
block 2)
for item in items:
  cars = db.GqlQuery('SELECT * FROM Car WHERE item=:1',item)
  items.update({item:cars.price})

我想要得到的是:

items = {"BE1234":["2","100"],"BE1923":["31","200"],"BE2032":["19","300"], etc}

更新(最终工作代码)

for item in items:
  cars = db.GqlQuery('SELECT * FROM Car WHERE item=:1',item)
  for car in cars:
    items[item]=[items[item],str(car.price)]
4

1 回答 1

3

构造一个要插入的列表(值)并仅更新键的值: items["BE1234"] = ["2", "100"]

详细(Shiva 的评论有一种“更清洁”的方式来做同样的事情):

current_val = items["BE1234"]
new_val = "100";
new_array = [current_val, new_val]
items["BE1234"] = new_array
于 2012-04-29T06:21:08.517 回答