将IntegerProperty更改为现有AppEngine DataStore的FloatProperty [英] Change IntegerProperty to FloatProperty of existing AppEngine DataStore
问题描述
我构建了一个appengine应用程序(python),它需要将整数值(100)中的现有数据存储实体转换为浮点值(100.00)以便进行货币转换问题。
如何以正确的方式做到这一点?由于我查询返回错误,当我只是改变我的模型中的属性类型。
旧模型:
class Learn(search.SearchableModel):
pid = db.ReferenceProperty(Product,collection_name ='picks')
title = db.StringProperty( )
description = db.TextProperty()
order = db.IntegerProperty()
cost = db.InteProProperty(default = 0)
cost1 = db.IntegerProperty(default = 0) )
新模型:
class Learn(search.SearchableModel):
pid = db.ReferenceProperty(Product,collection_name ='picks')
title = db.StringProperty( )
description = db.TextProperty()
order = db.InteProProperty()
cost = db.FloatProperty(default = 0.000)
cost1 = db.FloatProperty(default = 0.000) )
我需要一种正确的方式来更改此数据存储区属性类型而不更改(删除旧的并添加新的)现有数据。因为它是许多其他表格/模型的关键。
谢谢。
最简单的方法是将模型更改为从db.Expando继承,并从定义中删除整型属性。然后,加载每个实例并在每个实例上执行instance.foo = float(instance.foo),然后将它们保存回数据存储区 - 您可能需要为此使用mapreduce API。最后,让模型再次扩展db.Model,然后添加FloatProperties。
你真的,真的不想使用浮动货币,虽然:浮动易受舍入错误的影响,这意味着你可能会失去(或获得)钱。相反,使用一个IntegerProperty来计算分数。
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's the right way doing this? Since my query returning error when i just change property type in my model.
Old Model:
class Learn(search.SearchableModel):
pid = db.ReferenceProperty(Product, collection_name='picks')
title = db.StringProperty()
description = db.TextProperty()
order = db.IntegerProperty()
cost = db.IntegerProperty(default=0)
cost1 = db.IntegerProperty(default=0)
New Model:
class Learn(search.SearchableModel):
pid = db.ReferenceProperty(Product, collection_name='picks')
title = db.StringProperty()
description = db.TextProperty()
order = db.IntegerProperty()
cost = db.FloatProperty(default=0.000)
cost1 = db.FloatProperty(default=0.000)
I need a proper way to alter this datastore property type without changing (delete old and add new) existing data. Since it's key used in many others table/model.
Thanks.
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.
这篇关于将IntegerProperty更改为现有AppEngine DataStore的FloatProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!