AppEngine boot container export model with self-defined property

I want to use bulkloader to load all the objects in a model with some self- defined property.

If I define such a model,

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 load MyModel using the bootloader so that there is no untagged value in the file? I think I should define export_transform for info in bulkloader.yaml, but I don't know how it should be.

 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 saw transform.py but still don't know how this works. Please tell me any method that can solve my problem. Thanks.

+2
source share
1 answer

OK, I answer my questions ...

I still don't know why pickle doesn't work, but after changing the use of simplejson instead of pickle I can successfully export MyProperty to the specified format.

The .yaml bulk loader might look like this.

 python_preamable: - import myutils - import django.utils.simplejson ... transformers: - kind: MyModel connector: csv property_map: ... - property: info external_name: info export_transform: myutils.load_info 

And in myutils.py, load_info might look like this.

 def load_info(): def load(x): if not x: return '' info = simplejson.loads(x) return '%s-%s' % (info['id'], info['name']) # the output format for info return load 
+1
source

Source: https://habr.com/ru/post/910023/


All Articles