Skip to content

Instantly share code, notes, and snippets.

@vrySantosh
Created September 14, 2017 10:08
Show Gist options
  • Save vrySantosh/4c55496937c28ed2d9ab3aebee5bcfea to your computer and use it in GitHub Desktop.
Save vrySantosh/4c55496937c28ed2d9ab3aebee5bcfea to your computer and use it in GitHub Desktop.
It is a syntax sugar for faster query writing. Its implementation in pseudocode:
def filter_by(self, **kwargs):
return self.filter(sql.and_(**kwargs))
For AND you can simply write:
Users.query.filter_by(name='Joe', surname='Dodson')
btw
db.users.filter(or_(db.users.name=='Ryan', db.users.country=='England'))
can be written as
db.users.filter((db.users.name=='Ryan') | (db.users.country=='England'))
Also you can get object directly by PK via get method:
Users.query.get(123)
# And even by a composite PK
Users.query.get(123, 321)
When using get case its important that object can be returned without database request from identity map which can be used as cache(associated with transaction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment