Set a kind name independently of the model name (App Engine datastore)

前端 未结 1 1703
Happy的楠姐
Happy的楠姐 2021-01-14 03:50

As a Python programmer, I like my code to be reusable, I\'m trying to avoid kind name conflicts in my code (where two different models share the same kind name).

Cur

1条回答
  •  隐瞒了意图╮
    2021-01-14 04:19

    Just override the kind() method of your class:

    class MyModel(db.Model):
      @classmethod
      def kind(cls):
        return 'prefix_%s' % super(MyModel, cls).kind()
    

    You can define a custom baseclass that does this for you:

    class ModuleModel(db.Model):
      @classmethod
      def kind(cls):
        return '%s_%s' % (cls.__module__, super(ModuleModel, cls).kind())
    

    Any class that extends ModuleModel will have the name of the module it's defined in prefixed to the kind name.

    0 讨论(0)
提交回复
热议问题