GAE db.Model获取属性而不是字符串值

时间:2010-03-09 17:37:20

标签: python google-app-engine

我有一个db.Model,上面有一个字符串属性,email_type。现在我已经在readonly类中定义了类型的值。当我将其保存到数据存储区时,我得到字符串而不是“Register”,它也会引发BadValueError。如何将其保存为字符串,而不是属性。

这是(精简的)代码:

class EmailTypes(object):

    def __init__(self):
        self.__reg = "Register"
        self.__news = "NewsLetter"

    @property
    def Register(self):
        return self.__reg

    @property
    def NewsLetter(self):
        return self.__news

class Email(db.Model):
    to = db.StringProperty()
    email_type = db.StringProperty()


class Example(object)

    def do_stuff(self):
        e = Email()
        e.to = 'phil@somedomain.com'
        # This should be saving as 'Register' not a ref to the objects address
        e.email_type = EmailTypes().Register

do = Example()
do.do_stuff()

1 个答案:

答案 0 :(得分:3)

如果您将EmailTypes类更改为如下所示会发生什么:

class EmailTypes(object):
    Register = 'Register'
    NewsLetter = 'NewsLetter'

并使用它:

e.email_type = EmailTypes.Register

这会使您的简化示例有效吗?