views:

41

answers:

2

I have a db.Model which has a string property on it, email_type. Now I've the values for type defined in a readonly class. When I save this to the datastore I get the string instead of "Register", it also raises a BadValueError. How do I get it to save as a string, not as a property.

Here's the (slimmed down) code:

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 = '[email protected]'
        # This should be saving as 'Register' not a ref to the objects address
        e.email_type = EmailTypes().Register

do = Example()
do.do_stuff()
A: 
e.email_type = EmailTypes().Register

That only gives you the method/property object. You should call it like this to get the value:

e.email_type = EmailTypes().Register()
tomlog
That's not how it should work, since `EmailTypes.Regster` is made into a read-only property with the `@property` decorator. And anyway, the example code he posted works fine.
Will McCutchen
I think you might be right there. I haven't used the @property decorator myself, so I wasn't aware of that.
tomlog
+3  A: 

What happens if you change your EmailTypes class to look like this:

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

and use it like:

e.email_type = EmailTypes.Register

Does that make your simplified example work?

Will McCutchen
Phil's code does seem very over-complicated for the problem that it is trying to solve.
Adam Crossland
@Adam - It's a simplified version - I could give you the actual problem but it seemed like overkill. All I really want to know is how to return a string instead of a reference to the property object.
Phil
@Will - yes it would but I need the `Register`, `NewsLetter` and other properties read-only.
Phil
@Phil - I think you'll have to post your more complicated code, because the simplified example you posted works perfectly for me.
Will McCutchen
Yeah, I'll go rework this and see what I'm doing wrong. If I can't get it working I'll post the actual code. Thanks to everybody who replied.
Phil