Change IntegerProperty to FloatProperty of existing AppEngine DataStore

前端 未结 4 574
攒了一身酷
攒了一身酷 2021-01-06 01:31

I built an appengine application (python) which need to convert existing datastore entities in integer value (100) to float value (100.00) for currency conversion issue. How

4条回答
  •  悲&欢浪女
    2021-01-06 02:08

    The easiest way to do this is to change the model to inherit from db.Expando, and delete the integer properties from the definion. Then, load each instance and do "instance.foo = float(instance.foo)" on each, before saving them back to the datastore - you'll probably want to use the mapreduce API for this. Finally, make the model extend db.Model again, and add the FloatProperties back.

    You really, really don't want to use a float for currency, though: floats are susceptible to rounding errors, which means you can lose (or gain!) money. Instead, use an IntegerProperty that counts the number of cents.

提交回复
热议问题