Re: [sqlalchemy] [Q] Transform a select part of a query
Cool! That is what I was looking for. Thank you, Ladislav Lenart On 24.10.2012 20:00, Michael Bayer wrote: with_entities() will give you this: http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html#sqlalchemy.orm.query.Query.with_entities On Oct 24, 2012, at 11:21 AM, Ladislav Lenart wrote: Oh, you are right of course. This works, however I would still like to transform the query programmatically (mainly out of curiosity) from q = session.query(Person).order_by(Person.name) q = q.filter(Person.age age) return q to q = session.query(func.count(Person.id)) q = q.filter(Person.age age) return q (I am not sure if the order_by part can remain or not.) Is this possible somehow? I am pretty sure I read something along these lines but cannot find it anywhere. Ladislav Lenart On 24.10.2012 17:10, Simon King wrote: On Wed, Oct 24, 2012 at 3:59 PM, Ladislav Lenart lenart...@volny.cz wrote: Hello. Suppose I have the following query: def people_older_than(age): q = session.query(Person).order_by(Person.name) q = q.filter(Person.age age) return q It returns a subset of Person instances. How can I augment the query so it returns only their count? I.e.: def number_of_people_older_than(age) q = people_older_than(21) q_count = q# Apply some magic here. return q_count.scalar() Thank you in advance, Ladislav Lenart I don't think any magic is required. Does this work for you: def number_of_people_older_than(age) q = people_older_than(age) return q.count() See also http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html#counting Simon -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] [Q] Transform a select part of a query
On Wed, Oct 24, 2012 at 3:59 PM, Ladislav Lenart lenart...@volny.cz wrote: Hello. Suppose I have the following query: def people_older_than(age): q = session.query(Person).order_by(Person.name) q = q.filter(Person.age age) return q It returns a subset of Person instances. How can I augment the query so it returns only their count? I.e.: def number_of_people_older_than(age) q = people_older_than(21) q_count = q# Apply some magic here. return q_count.scalar() Thank you in advance, Ladislav Lenart I don't think any magic is required. Does this work for you: def number_of_people_older_than(age) q = people_older_than(age) return q.count() See also http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html#counting Simon -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] [Q] Transform a select part of a query
Oh, you are right of course. This works, however I would still like to transform the query programmatically (mainly out of curiosity) from q = session.query(Person).order_by(Person.name) q = q.filter(Person.age age) return q to q = session.query(func.count(Person.id)) q = q.filter(Person.age age) return q (I am not sure if the order_by part can remain or not.) Is this possible somehow? I am pretty sure I read something along these lines but cannot find it anywhere. Ladislav Lenart On 24.10.2012 17:10, Simon King wrote: On Wed, Oct 24, 2012 at 3:59 PM, Ladislav Lenart lenart...@volny.cz wrote: Hello. Suppose I have the following query: def people_older_than(age): q = session.query(Person).order_by(Person.name) q = q.filter(Person.age age) return q It returns a subset of Person instances. How can I augment the query so it returns only their count? I.e.: def number_of_people_older_than(age) q = people_older_than(21) q_count = q# Apply some magic here. return q_count.scalar() Thank you in advance, Ladislav Lenart I don't think any magic is required. Does this work for you: def number_of_people_older_than(age) q = people_older_than(age) return q.count() See also http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html#counting Simon -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] [Q] Transform a select part of a query
with_entities() will give you this: http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html#sqlalchemy.orm.query.Query.with_entities On Oct 24, 2012, at 11:21 AM, Ladislav Lenart wrote: Oh, you are right of course. This works, however I would still like to transform the query programmatically (mainly out of curiosity) from q = session.query(Person).order_by(Person.name) q = q.filter(Person.age age) return q to q = session.query(func.count(Person.id)) q = q.filter(Person.age age) return q (I am not sure if the order_by part can remain or not.) Is this possible somehow? I am pretty sure I read something along these lines but cannot find it anywhere. Ladislav Lenart On 24.10.2012 17:10, Simon King wrote: On Wed, Oct 24, 2012 at 3:59 PM, Ladislav Lenart lenart...@volny.cz wrote: Hello. Suppose I have the following query: def people_older_than(age): q = session.query(Person).order_by(Person.name) q = q.filter(Person.age age) return q It returns a subset of Person instances. How can I augment the query so it returns only their count? I.e.: def number_of_people_older_than(age) q = people_older_than(21) q_count = q# Apply some magic here. return q_count.scalar() Thank you in advance, Ladislav Lenart I don't think any magic is required. Does this work for you: def number_of_people_older_than(age) q = people_older_than(age) return q.count() See also http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html#counting Simon -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.