Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_models should support OR queries #17

Closed
jace opened this issue Dec 7, 2012 · 3 comments
Closed

load_models should support OR queries #17

jace opened this issue Dec 7, 2012 · 3 comments

Comments

@jace
Copy link
Member

jace commented Dec 7, 2012

load_models currently treats all parameters as an AND query when loading a model. It needs the ability to perform an OR query. Consider this:

@app.route('/<channel>/<playlist>')
@load_models(
    (Channel, {'name': 'channel'}, 'channel'),
    (Playlist, {'name': 'playlist', 'channel': 'channel'}, 'playlist')
)
def playlist_view(channel, playlist):
    pass

The dictionary syntax treats all dictionary keys as part of an AND query. It should be possible to perform an OR query, checking for a match in one or more attributes. I propose this syntax:

@app.route('/<channel>/<playlist>')
@load_models(
    (Channel, {'name': 'channel'}, 'channel'),
    (Playlist, ({'name': 'playlist', 'channel': 'channel'}, {'uuid': 'playlist', 'channel': 'channel'}), 'playlist')
)
def playlist_view(channel, playlist):
    pass

Supplying a list/tuple of dictionaries should signal an OR query.

@jace
Copy link
Member Author

jace commented Dec 7, 2012

Alternate syntax for nested AND/OR clauses:

@app.route('/<channel>/<playlist>')
@load_models(
    (Channel, {'name': 'channel'}, 'channel'),
    (Playlist, {('name', 'uuid'): 'playlist', 'channel': 'channel'}, 'playlist')
)
def playlist_view(channel, playlist):
    pass

This is possible because tuples can be dictionary keys.

Both variations of the OR syntax introduce a new problem: we currently apply the filter using filter_by, which takes string names. Using or_ (and and_) will require using filter, which requires Python expressions. This means a previously simple call:

column = 'name'

Model.query.filter_by(**{column: value})

Now looks like this

column = 'name'

Model.query.filter(getattr(Model, column) == value)

@jace
Copy link
Member Author

jace commented Nov 21, 2017

This getattr call is in fact what the filter_by method does. It's just syntactic sugar for it.

@jace
Copy link
Member Author

jace commented Feb 27, 2018

This is now deprecated with ClassView ( #167), where the class can specify custom loaders. That will be far more efficient than load_models supporting additional syntaxes.

@jace jace closed this as completed Feb 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant