Question

I've been trying to add a value to a pre-existing dictionary key. The value comes from the db.Model Car with the property named price.

Tried two blocks of code (below) but have been getting the 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})

What I'm looking to get is:

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

Update (final working code)

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)]
Was it helpful?

Solution

Construct a list (value) to be inserted and just update the value of a key: items["BE1234"] = ["2", "100"]

detailed (Shiva's comment has a "cleaner" way of doing the same thing):

current_val = items["BE1234"]
new_val = "100";
new_array = [current_val, new_val]
items["BE1234"] = new_array
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top