How to shallow copy app engine model instance to create new instance?

你说的曾经没有我的故事 提交于 2019-12-08 10:32:36

问题


I want to implement a simple VersionedModel base model class for my app engine app. I'm looking for a pattern that does not involve explicitly choosing fields to copy.

I am trying out something like this, but it is to hacky for my taste and did not test it in the production environment yet.

class VersionedModel(BaseModel):
    is_history_copy     = db.BooleanProperty(default=False)
    version             = db.IntegerProperty()
    created             = db.DateTimeProperty(auto_now_add=True)
    edited              = db.DateTimeProperty()
    user                = db.UserProperty(auto_current_user=True)

    def put(self, **kwargs):
        if self.is_history_copy:
            if self.is_saved():
                raise Exception, "History copies of %s are not allowed to change" % type(self).__name__
            return super(VersionedModel, self).put(**kwargs)
        if self.version is None:
            self.version = 1
        else:
            self.version = self.version +1
        self.edited =  datetime.now() # auto_now would also affect copies making them out of sync
        history_copy = copy.copy(self)
        history_copy.is_history_copy = True
        history_copy._key = None
        history_copy._key_name = None
        history_copy._entity = None
        history_copy._parent = self
        def tx():
            result = super(VersionedModel, self).put(**kwargs)
            history_copy._parent_key = self.key()
            history_copy.put()
            return result
        return db.run_in_transaction(tx)

Does anyone have a simpler cleaner solution for keeping history of versions for app engine models?

EDIT: Moved copy out of tx. Thx @Adam Crossland for the suggestion.


回答1:


Take a look at the properties static method on Model classes. With this, you can get a list of properties, and use that to get their values, something like this:

  @classmethod
  def clone(cls, other, **kwargs):
    """Clones another entity."""
    klass = other.__class__
    properties = other.properties().items()
    kwargs.update((k, p.__get__(other, klass)) for k, p in properties)
    return cls(**kwargs)


来源:https://stackoverflow.com/questions/3691064/how-to-shallow-copy-app-engine-model-instance-to-create-new-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!