具有自定义属性的 AppEnginebulkloader 导出模型
我想使用bulkloader来下载模型中具有一些自定义属性的所有实体。
如果我定义这样的模型,
class MyType:
def __init__(self, arg):
self.name = arg['name']
self.id = arg['id']
class MyProperty(db.Property):
def get_value_for_datastore(self, instance):
val = super(MyProperty, self).get_value_for_datastore(instance)
if type(val) == dict:
val = MyType(val)
return pickle.dumps(val)
def make_value_from_datastore(self, val):
return None if val is None else pickle.loads(str(val))
class MyModel(db.Model):
info = MyProperty()
那么如何使用批量加载器下载MyModel
,以便文件中不会有未腌制的值?我想我应该在bulkloader.yaml中为info
定义export_transform
,但我不知道它应该是什么样的。
transformers:
- kind: MyModel
connector: csv
property_map:
- property: __key__
external_name: log_id
export_transform: transform.key_id_or_name_as_string
- property: info
external_name: info
export_transform: ### HERE ###
我见过 transform.py 但仍然不知道它是如何工作的。请告诉我任何可以解决我的问题的方法。谢谢。
I want to use bulkloader to download all entities in a model with some self-defined Property.
If I define a model like this,
class MyType:
def __init__(self, arg):
self.name = arg['name']
self.id = arg['id']
class MyProperty(db.Property):
def get_value_for_datastore(self, instance):
val = super(MyProperty, self).get_value_for_datastore(instance)
if type(val) == dict:
val = MyType(val)
return pickle.dumps(val)
def make_value_from_datastore(self, val):
return None if val is None else pickle.loads(str(val))
class MyModel(db.Model):
info = MyProperty()
then how can I download MyModel
using the bulkloader such that there will not be un-pickled value in the file? I think I should define the export_transform
for info
in bulkloader.yaml, but I don't know what it should be like.
transformers:
- kind: MyModel
connector: csv
property_map:
- property: __key__
external_name: log_id
export_transform: transform.key_id_or_name_as_string
- property: info
external_name: info
export_transform: ### HERE ###
I've seen transform.py but still have no idea about how it works. Please tell my any method that can solve my problem. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我正在回答我自己的问题...
我仍然不知道为什么
pickle
不起作用,但是在更改为使用simplejson
而不是pickle 之后
,我可以成功以指定格式导出MyProperty
。bulkloader.yaml 可能如下所示。
在 myutils.py 中
load_info
可能看起来像这样。Okay I'm answering my own questions...
I still don't know why
pickle
doesn't work, but after changing to usesimplejson
instead ofpickle
, I can successfully exportMyProperty
in the designated format.The bulkloader.yaml may look like this.
And in myutils.py
load_info
may look like this.