设置一个与模型名称(App Engine数据存储)无关的名称 [英] Set a kind name independently of the model name (App Engine datastore)
问题描述
目前,我只是在模型的类名前添加了一些有意义的文本,但是这非常合理。
能够明确设置模型的类型将解决我的问题问题,但我无法找到如何做到这一点,有谁知道如何?
解决方案只要覆盖 kind()
类的方法:
class MyModel(db.Model):
@classmethod
def kind(cls):
return'prefix_%s'%super(MyModel,cls).kind()
你可以定义一个自定义的基类来为你做这件事:
class ModuleModel(db.Model):
@classmethod
def kind(cls):
return'%s_%s'%(cls .__ module__,super(ModuleModel,cls))。 kind())
扩展ModuleModel的任何类将具有其名称前缀中定义的模块的名称。
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).
Currently I just prepend some meaningful text to the model's class name, but this is awfully unpythonic.
Being able to explicitly set the model's kind will solve my problem, but I can't find out how to do this, does anyone know how?
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.
这篇关于设置一个与模型名称(App Engine数据存储)无关的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!