我刚从sqlalchemy的CRM应用程序中反思了一个非常讨厌的架构。所有表都有一个已删除的列,我想自动过滤所有标记为已删除的实体和关系。这就是我想出的:
class CustomizableQuery(Query):
"""An overridden sqlalchemy.orm.query.Query to filter entities
Filters itself by BinaryExpressions
found in :attr:`CONDITIONS`
"""
CONDITIONS = []
def __init__(self, mapper, session=None):
super(CustomizableQuery, self).__init__(mapper, session)
for cond in self.CONDITIONS:
self._add_criterion(cond)
def _add_criterion(self, criterion):
criterion = self._adapt_clause(criterion, False, True)
if self._criterion is not None:
self._criterion = self._criterion & criterion
else:
self._criterion = criterion
它的使用方式如下:
class UndeletedContactQuery(CustomizableQuery):
CONDITIONS = [contacts.c.deleted != True]
def by_email(self, email_address):
return EmailInfo.query.by_module_and_address('Contacts', email_address).contact
def by_username(self, uname):
return self.filter_by(twod_username_c=uname).one()
class Contact(object):
query = session.query_property(UndeletedContactQuery)
Contact.query.by_email('someone@some.com')
EmailInfo是映射到电子邮件与其相关的其他模块之间的连接表的类。
以下是映射器的示例:
contacts_map = mapper(Contact, join(contacts, contacts_cstm), {
'_emails': dynamic_loader(EmailInfo,
foreign_keys=[email_join.c.bean_id],
primaryjoin=contacts.c.id==email_join.c.bean_id,
query_class=EmailInfoQuery),
})
class EmailInfoQuery(CustomizableQuery):
CONDITIONS = [email_join.c.deleted != True]
# More methods here
这给了我想要的东西,我已经过滤掉了所有已删除的联系人。我也可以使用它作为我的映射器中dynamic_loader的query_class参数 - 但是......
答案 0 :(得分:7)
您可以映射到选择。像这样:
mapper(EmailInfo, select([email_join], email_join.c.deleted == False))
答案 1 :(得分:0)
我会考虑是否可以为这些过滤掉已删除元素的表创建视图,然后您可以直接映射到该视图而不是基础表,至少对于查询操作而言。但是我自己从未尝试过这个!