Python 将现有AppEngine数据存储的IntegerProperty更改为FloatProperty
我构建了一个appengine应用程序(python),它需要将整数值(100)中的现有数据存储实体转换为浮点值(100.00),以便进行货币转换。 这样做的正确方法是什么?因为我刚更改模型中的属性类型时,查询返回错误 旧型号:Python 将现有AppEngine数据存储的IntegerProperty更改为FloatProperty,python,django,google-app-engine,google-cloud-datastore,Python,Django,Google App Engine,Google Cloud Datastore,我构建了一个appengine应用程序(python),它需要将整数值(100)中的现有数据存储实体转换为浮点值(100.00),以便进行货币转换。 这样做的正确方法是什么?因为我刚更改模型中的属性类型时,查询返回错误 旧型号: class Learn(search.SearchableModel): pid = db.ReferenceProperty(Product, collection_name='picks') title = db.StringProperty()
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)
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)
新型号:
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)
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)
我需要一种正确的方法来更改此数据存储属性类型,而不更改(删除旧数据和添加新数据)现有数据。因为它是许多其他表/模型中使用的键
谢谢。也许一个好方法是临时创建一个新模型:
class LearnTemp(search.SearchableModel):
pid = db.ReferenceProperty(Product, collection_name='picks')
title = db.StringProperty()
description = db.TextProperty()
order = db.IntegerProperty()
order = db.IntegerProperty()
cost = db.FloatProperty(default=0.000)
cost1 = db.FloatProperty(default=0.000)
然后编写一些脚本、任务或视图,将实例从旧模型转换为临时模型,将整数值转换为浮点值。如果可能,请确保复制id和密钥
更改主模型后,将所有条目从临时模型复制到主模型。然后删除临时模型
这很可能不是最佳方法,需要一些手动迁移,但如果没有South和on app engine,我真的看不到一个好方法。最简单的方法是将模型更改为从db.Expando继承,并从定义中删除整数属性。然后,加载每个实例并在每个实例上执行“instance.foo=float(instance.foo)”,然后再将它们保存回数据存储—您可能需要为此使用mapreduce API。最后,使模型再次扩展db.model,并重新添加FloatProperties
不过,你真的真的不想使用浮动汇率:浮动汇率容易出现舍入错误,这意味着你可能会损失(或获得!)金钱。相反,使用整数属性来计算美分数。从数据存储管理界面的编辑实体页面: 输入实体的信息 在下面如果你想换一个 属性的类型,将其设置为Null,保存 实体,再次编辑该实体,然后 更改类型
以下是尼克·约翰逊的例子:
之前
:
class Person(db.Model):
name = db.StringProperty()
age = db.StringProperty() #this will go to int
之后
class Person(db.Expando):
pass
for person in Person.all():
person.age = int(person.age)
person.put()
非常后
:
class Person(db.Model):
name = db.StringProperty()
age = db.IntegerProperty()
再次感谢Nick,考虑货币表的整数+美分。@Ivan我不是说两个整数-虽然这也行-我只是指将美分的总数存储在一个字段中。例如,10.23美元存储为1023美元。