Skip to content

Commit

Permalink
Add Model classmethod proxies to Model.query.filter and Model.query.f…
Browse files Browse the repository at this point in the history
…ilter_by.
  • Loading branch information
dgilland committed Dec 17, 2014
1 parent 2d6ddba commit 4e9a2f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions alchy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ def get_by(cls, data_dict=None, **kargs):
data = data_dict if isinstance(data_dict, dict) else kargs
return cls.query.filter_by(**data).first()

@classmethod
def filter(cls, *args, **kargs):
"""Proxy to ``cls.query.filter()``"""
return cls.query.filter(*args, **kargs)

@classmethod
def filter_by(cls, *args, **kargs):
"""Proxy to ``cls.query.filter_by()``"""
return cls.query.filter_by(*args, **kargs)

##
# SQLAlchemy.inspect() based methods/properties
##
Expand Down
7 changes: 7 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ def test_get_by(self):
Foo.get_by(dict(string='Joe Smith')),
self.db.query(Foo).filter_by(string='Joe Smith').first())

def test_filter(self):
self.assertEqual(str(Foo.filter()), str(self.db.query(Foo).filter()))

def test_filter_by(self):
self.assertEqual(str(Foo.filter_by()),
str(self.db.query(Foo).filter_by()))

def test_object_session(self):
record = Foo.get(1)
self.assertIs(record.object_session,
Expand Down

0 comments on commit 4e9a2f5

Please sign in to comment.