With this code:
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.schema import Column
from sqlalchemy.types import Integer, String
Base = declarative_base()
class Mixin(object):
@declared_attr
def attr2(cls):
return Column(String(20), nullable=False)
class Test(Base, Mixin):
__tablename__ = 'test'
id = Column(Integer, primary_key=True)
@declared_attr
def attr1(cls):
return Column(String(20), nullable=False)
if __name__ == '__main__':
print Test.attr1.__class__
print Test.attr2.__class__
Test.attr1 will be a sqlalchemy.schema.Column, while Test.attr2 will
be a sqlalchemy.orm.attributes.InstrumentedAttribute. Why are they
behave differently?
Anyway, what I want to achieve is to selectively define a column based
on some external flag, so I was trying to put in if..else block inside
@declared_attr to return either None or Column. Is there a better way
to do it, e.g. using a metaclass?
Thanks.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.