From 4e9a2f5948377ff421bfccb7b2c1ccf846e9b6f9 Mon Sep 17 00:00:00 2001 From: Derrick Gilland Date: Tue, 16 Dec 2014 22:51:06 -0500 Subject: [PATCH] Add Model classmethod proxies to Model.query.filter and Model.query.filter_by. --- alchy/model.py | 10 ++++++++++ tests/test_model.py | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/alchy/model.py b/alchy/model.py index 908ddc6..c09b2dd 100644 --- a/alchy/model.py +++ b/alchy/model.py @@ -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 ## diff --git a/tests/test_model.py b/tests/test_model.py index 5464d31..b2fc887 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -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,