Skip to content

Instantly share code, notes, and snippets.

@NotTheEconomist
Created June 3, 2015 16:15
Show Gist options
  • Save NotTheEconomist/0dc7ab0ee5270fc69cfa to your computer and use it in GitHub Desktop.
Save NotTheEconomist/0dc7ab0ee5270fc69cfa to your computer and use it in GitHub Desktop.
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