QueryChain is a wrapper for sequence of queries.
Features:
- Easy iteration for sequence of queries
- Limit, offset and count which are applied to all queries in the chain
- Smart __getitem__ support
QueryChain takes iterable of queries as first argument. Additionally limit and offset parameters can be given
chain = QueryChain([session.query(User), session.query(Article)])
chain = QueryChain(
[session.query(User), session.query(Article)],
limit=4
)
chain = QueryChain([session.query(User), session.query(Article)])
for obj in chain:
print obj
Lets say you have 5 blog posts, 5 articles and 5 news items in your database.
chain = QueryChain(
[
session.query(BlogPost),
session.query(Article),
session.query(NewsItem)
],
limit=5
)
list(chain) # all blog posts but not articles and news items
chain = chain.offset(4)
list(chain) # last blog post, and first four articles
Just like with original query object the limit and offset can be chained to return a new QueryChain.
chain = chain.limit(5).offset(7)
chain = QueryChain(
[
session.query(BlogPost),
session.query(Article),
session.query(NewsItem)
]
)
chain[3:6] # New QueryChain with offset=3 and limit=6
Let’s assume that there are five blog posts, five articles and five news items in the database, and you have the following query chain:
chain = QueryChain(
[
session.query(BlogPost),
session.query(Article),
session.query(NewsItem)
]
)
You can then get the total number rows returned by the query chain with count():
>>> chain.count()
15
QueryChain can be used as a wrapper for sequence of queries.
Parameters: |
|
---|