On 05/25/2016 02:38 PM, Andrew Pashkin wrote:
On 05/25/2016 09:28 PM, Mike Bayer wrote:
why do you even need this?
Basically I have a REST API for which I want to add functionality of
filtering and sorting, according to user provided parameters. User
supposed to provide a field name and the magic function should resolve
this field name in a given query to an unambiguous column reference, to
use it for sorting and filtering.

Well first off, your original example that uses joinedload(), you will never get a hold of anything from Bar, so for that, feel free to order by the single entity based on name. Core / ORM handles this case automatically as of 1.0:

q = session.query(Foo).options(joinedload('bars'))
q = q.order_by("name")


output:

SELECT foo.id AS foo_id, foo.name AS foo_name, bar_1.id AS bar_1_id, bar_1.bar_id AS bar_1_bar_id FROM foo LEFT OUTER JOIN bar AS bar_1 ON foo.id = bar_1.bar_id ORDER BY foo.name

Similarly, use filter_by(name='x'), again, Bar is part of joinedload(), you can't do anything to it:


q = session.query(Foo).options(joinedload('bars'))
q = q.order_by(name='foob')
q = q.order_by("name")


output:

SELECT foo.id AS foo_id, foo.name AS foo_name, bar_1.id AS bar_1_id, bar_1.bar_id AS bar_1_bar_id
FROM foo LEFT OUTER JOIN bar AS bar_1 ON foo.id = bar_1.bar_id
WHERE foo.name = ? ORDER BY foo.name

there's a lot more that can be done here but if you're querying against a single entity, its attribute names are first class referenceable.

Assuming you want more, phrase this as an input/output situation for me. Give me input and what the desired output you want is. The big guns here are a function called corresponding_column() which I can show you how to use if that's what's needed.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to