Skip to content

Commit

Permalink
Expose BaseQuery as db.Query (fixes ticket #13).
Browse files Browse the repository at this point in the history
  • Loading branch information
miracle2k authored and Ron DuPlain committed Mar 14, 2011
1 parent 9cbe5bc commit 7707529
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion docs/index.rst
Expand Up @@ -211,6 +211,10 @@ Configuration
.. autoclass:: SQLAlchemy
:members:

.. attribute:: Query

The :class:`BaseQuery` class.

Models
``````

Expand All @@ -231,7 +235,7 @@ Models
newly resulting query.

.. method:: limit(limit)

Apply a LIMIT to the query and return the newly resulting query.

.. method:: offset(offset)
Expand Down
4 changes: 3 additions & 1 deletion flaskext/sqlalchemy.py
Expand Up @@ -333,7 +333,8 @@ def iter_pages(self, left_edge=2, left_current=2,


class BaseQuery(orm.Query):
"""The default query object used for models. This can be subclassed and
"""The default query object used for models, and exposed as
:attr:`~SQLAlchemy.Query`. This can be subclassed and
replaced for individual models by setting the :attr:`~Model.query_class`
attribute. This is a subclass of a standard SQLAlchemy
:class:`~sqlalchemy.orm.query.Query` class and has all the methods of a
Expand Down Expand Up @@ -544,6 +545,7 @@ def __init__(self, app=None, use_native_unicode=True,
self.app = None

_include_sqlalchemy(self)
self.Query = BaseQuery

@property
def metadata(self):
Expand Down
15 changes: 15 additions & 0 deletions test_sqlalchemy.py
Expand Up @@ -210,5 +210,20 @@ class Child(db.Model):
self.assert_(isinstance(c.parents, sqlalchemy.BaseQuery))


class SQLAlchemyIncludesTestCase(unittest.TestCase):

def test(self):
"""Various SQLAlchemy objects are exposed as attributes.
"""
db = sqlalchemy.SQLAlchemy()

import sqlalchemy as sqlalchemy_lib
self.assertTrue(db.Column == sqlalchemy_lib.Column)

# The Query object we expose is actually our own subclass.
from flaskext.sqlalchemy import BaseQuery
self.assertTrue(db.Query == BaseQuery)


if __name__ == '__main__':
unittest.main()

0 comments on commit 7707529

Please sign in to comment.