2 つのテーブル間にリレーションシップを作成しようとしています。
モデルをインスタンス化すると、次のエラーが発生します。
from core.models import Activation
a = Activation()
ArgumentError: Class object expected, got 'Table(u'activations', MetaData(bind=Engine(postgresql+psycopg2://localhost:5432/mydb)), Column('id', Integer(), table=<activations>, primary_key=True, nullable=False), Column('valid_until', DateTime(), table=<activations>), Column('code', Unicode(length=30), table=<activations>, nullable=False), Column('created_by', Unicode(length=16), table=<activations>, nullable=False), schema=None)'.
コア/models.py
class ActivationMixin(Base):
@declared_attr
def code(self):
return Column(Unicode(30), nullable=False, unique=True)
@declared_attr
def valid_until(self):
return Column(DateTime, nullable=True)
@declared_attr
def created_by(self):
return Column(Unicode(16), nullable=False)
@classmethod
def get_by_code(cls, request, code):
session = get_session(request)
return session.query(cls).filter(cls.code == code).first()
def __init__(self, created_by='web', valid_until=None):
"""Create a new activation. *valid_until* is a datetime.
It defaults to 3 days from current day.
"""
self.code = generate_random_string(24)
self.created_by = created_by
if valid_until:
self.valid_until = valid_until
else:
self.valid_until = datetime.utcnow() + timedelta(days=3)
class Activation(ActivationMixin):
pass
ユーザー/models.py
class User(Base):
email = Column(Unicode(256), nullable=False, unique=True)
status = Column(Boolean, default=False)
salt = Column(Unicode(32), nullable=False)
_password = Column('password', Unicode(256), nullable=False)
logins = Column(Integer, default=0)
last_login = Column(
TIMESTAMP(timezone=False),
default=func.now(),
server_default=func.now(),
nullable=False
)
account_type = Column(AccountType.db_type())
@declared_attr
def activation_id(self):
return Column(
Integer,
ForeignKey('%s.id' % (Activation.__tablename__))
)
@property
def is_activated(self):
if self.activation_id is None:
return True
return False
@declared_attr
def activation(self):
return relationship(
Activation.__tablename__,
backref=self.__tablename__
)
エラーは、次の宣言で発生します。
@declared_attr
def activation(self):
return relationship(
Activation.__tablename__,
backref=self.__tablename__
)