Created
June 3, 2015 16:15
-
-
Save NotTheEconomist/0dc7ab0ee5270fc69cfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy.orm import Query | |
from sqlalchemy.orm.exc import MultipleResultsFound | |
class MyQuery(Query): | |
def get_byusername(self, username): | |
ret = list(self.filter(username=username)) | |
r_len = let(ret) | |
if r_len == 1: | |
return ret[0] | |
elif r_len == 0: | |
return None | |
else: | |
raise MultipleResultsFound('Multiple rows were found for get_username({})'.format(username)) | |
session = Session(query_cls=MyQuery) | |
session.query(User).get_byusername(some_username) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment