设置一个与模型名称(App Engine数据存储)无关的名称 [英] Set a kind name independently of the model name (App Engine datastore)

查看:86
本文介绍了设置一个与模型名称(App Engine数据存储)无关的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一名Python程序员,我喜欢我的代码是可重用的,我试图避免在我的代码中出现好的名称冲突(两个不同的模型共享相同的名称)。



目前,我只是在模型的类名前添加了一些有意义的文本,但是这非常合理。



能够明确设置模型的类型将解决我的问题问题,但我无法找到如何做到这一点,有谁知道如何?

解决方案

只要覆盖 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆