Search code examples
google-app-enginedatastore

db.expando + App Engine + Change an integer property value to float value


I've following model set up initially

class Obj (db.Model):
    name = db.StringProperty(required=True)
    rating = db.IntegerProperty(default=0, required=False)

There are entities already created with above, such as:

name="test1", rating="3"

So, I need to change the rating type to float. I was trying to achieve this with db.Expando

class Obj (db.Expando):
    name = db.StringProperty(required=True)
    rating = db.FloatProperty(default=0, required=False)

Before I'm able to retrieve the instance of the Obj model to update it to float value, I've already got the following error:

Property rating must be a float

At first, I got this error, because I wasn't using db.Expando. However, after using db.Expando, I assumed this error shouldn't come into place ? Since it can dynamically change value, type etc as I read the articles.

Being new to db.Expando, I need help. Does anyone have clue to what happened ?

EDIT

for o in Obj.all():
    a = []
    if o.rating:
        o.rating = float(str(restaurant.rating))
    else:
        o.rating = float(0)    
    a.append(restaurant)

db.put(a)

After having above code, the same error pops up

Property rating must be a float

SOLUTION Temporarily removed the rating from model definition and updated the values to float first and then add the new rating definition with db.FloatProperty


Solution

  • A db.Expando model allows you to add properties that aren't defined in the class itself; however, any properties that are explicitly defined in the class do need to be the correct type.

    Simply removing rating from the model definition may work for you.