0

我想创建一个本身继承自 db.Model 的基本枚举类。这个想法是创建几个可用于枚举类的任何后代的辅助函数。这些属性在某种程度上可能非常广泛,但是我可以在基枚举类中声明几个常见的属性(例如,“名称”)。在 App Engine 中是否有处理枚举模型的标准方法?

4

1 回答 1

2

看来您必须编写自己的属性类型类(您可能可以扩展 db.IntegerProperty)。Nick Johnson 的 ChoiceModel 类是如何做到这一点的一个例子:

  This works by mapping each choice to an integer.  The choices must be hashable
  (so that they can be efficiently mapped back to their corresponding index).

  Example usage:

  >>> class ChoiceModel(db.Model):
  ...   a_choice = ChoiceProperty(enumerate(['red', 'green', 'blue']))
  ...   b_choice = ChoiceProperty([(0,None), (1,'alpha'), (4,'beta')])

  You interact with choice properties using the choice values:

  >>> model = ChoiceModel(a_choice='green')
  >>> model.a_choice
  'green'
  >>> model.b_choice == None
  True
  >>> model.b_choice = 'beta'
  >>> model.b_choice
  'beta'

(链接代码和引用的评论是在 Apache 许可下发布的;版权所有 2011 Nick Johnson。)

于 2013-02-05T01:05:51.250 回答