下面是一個笨拙和低效的方式來實現,具體的結果,如果超過一個角色出現在目標用戶也將失敗:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
role_to_user = Table('role_to_user', Base.metadata,
Column('role_id', Integer, ForeignKey('role.id'), primary_key=True),
Column('user_id', Integer, ForeignKey('user.id'), primary_key=True),
)
class Role(Base):
__tablename__ = 'role'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
discrim = select([Role.name])
discrim_col = discrim.label("foo")
type = column_property(discrim_col)
roles = relationship("Role", secondary=role_to_user)
__mapper_args__ = {'polymorphic_on': discrim_col}
User.discrim.append_whereclause(Role.id==role_to_user.c.role_id)
User.discrim.append_whereclause(role_to_user.c.user_id==User.id)
class Customer(User):
__tablename__ = 'customer'
id = Column(Integer, ForeignKey(User.id), primary_key=True)
__mapper_args__ = {'polymorphic_identity':'customer'}
class Mechanic(User):
__tablename__ = 'mechanic'
id = Column(Integer, ForeignKey(User.id), primary_key=True)
__mapper_args__ = {'polymorphic_identity':'mechanic'}
e = create_engine('sqlite://', echo=True)
Base.metadata.create_all(e)
s = Session(e)
m, c = Role(name='mechanic'), Role(name='customer')
s.add_all([m, c])
s.add_all([Mechanic(roles=[m]), Customer(roles=[c])])
s.commit()
print Session(e).query(User).all()
接下來,這裏是我會怎麼真正做到這一點,如果我有一個基於零個或多個角色分配行爲的問題 - 我把行爲中的角色,角色的不是車主:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String)
roles = relationship("Role", secondary= Table(
'role_to_user', Base.metadata,
Column('role_id', Integer,
ForeignKey('role.id'),
primary_key=True),
Column('user_id', Integer,
ForeignKey('user.id'),
primary_key=True),
),
lazy="subquery"
)
def operate_on_roles(self):
for role in self.roles:
role.operate(self)
class Role(Base):
__tablename__ = 'role'
id = Column(Integer, primary_key=True)
type = Column(String, nullable=False)
__mapper_args__ = {'polymorphic_on': type}
class CustomerRole(Role):
__tablename__ = 'customer'
id = Column(Integer, ForeignKey(Role.id), primary_key=True)
__mapper_args__ = {'polymorphic_identity':'customer'}
def operate(self, user):
print "user %s getting my car fixed!" % user.name
class MechanicRole(Role):
__tablename__ = 'mechanic'
id = Column(Integer, ForeignKey(Role.id), primary_key=True)
__mapper_args__ = {'polymorphic_identity':'mechanic'}
def operate(self, user):
print "user %s fixing cars!" % user.name
e = create_engine('sqlite://', echo=True)
Base.metadata.create_all(e)
s = Session(e)
m, c = MechanicRole(), CustomerRole()
s.add_all([m, c])
s.add_all([
User(name='u1', roles=[m, c]),
User(name='u2', roles=[c]),
User(name='u3', roles=[m]),
])
s.commit()
for user in s.query(User):
user.operate_on_roles()
第二例產生輸出:
user u1 fixing cars!
user u1 getting my car fixed!
user u2 getting my car fixed!
user u3 fixing cars!
正如你所看到的第二個例子清晰高效,而第一個例子只是一個思想實驗。
有什麼建議嗎? – sasker 2011-05-27 05:08:43
如果用戶同時擁有「機械師」和「客戶」角色,該怎麼辦? – zzzeek 2011-06-11 18:27:25