setattr
allows you to dynamically name attributes in Python classes. I'm trying to do something similar with an App Engine Model:
class MyModel(db.Model):
def __init__(self, *args, **kwargs):
super(MyModel, self).__init__(*args, **kwargs)
# Doesn't fully work
setatr(self, 'prop1', db.ListProperty(db.Key))
setatr(self, 'prop2', db.StringListProperty())
# Works fully
# prop1 = db.ListProperty(db.Key))
# prop2 = db.StringListProperty())
This code compiles, but when I call model.prop1.append(key)
later on, I get this error:
AttributeError: 'ListProperty' object has no attribute 'append'
I suspect this is because prop1
is declared in models instead of self.prop1
, but I don't fully understand the syntax's significance.
Has anyone accomplished this, or does anyone have any insight into syntactic differences?
This SO question:
gives an example of what you want to do.