Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

How to query value in list? #44

Open
n3storm opened this issue Aug 19, 2017 · 3 comments
Open

How to query value in list? #44

n3storm opened this issue Aug 19, 2017 · 3 comments

Comments

@n3storm
Copy link

n3storm commented Aug 19, 2017

I think reading the code that lifter supports value_in lists query. I cannot find any example or tip on how to implement this in my filter.
Also startswith and other lookups would be nice to have them documented.

My data is a simple list of dictionaries, and list is a list.

Doesn't work:
sent_messages = objects.filter(folder__in = sent_folders)

Doesn't work:
sent_messages = objects.filter(Message.folder in sent_folders)

Combining Nodes section is not helpful either.

qn = (Message.date > this_year) & (Message.folder == 'INBOX.Sent') | (Message.folder == 'Sent') | (Message.folder == 'Enviados') | (Message.folder == 'INBOX.Enviados')
sent_query = lifter.query.Query(action='select', filters=qn)
sent_messages = objects.filter(sent_query)

Please help! :)

@agateblue
Copy link
Owner

Hello and sorry for my late answer, I was actually in a vacation trip :)

You can indeed use in, but with the myfield__value_in lookup:

objects.filter(eye_color__value_in=['brown'])

The fact is Python does not allows to override the __contains__ to return a non-boolean value. This is quite strange, considering you can do this for other lookups such as __eq__.

If you want to combine queries, you can also use the value_in operator object directly:

sent_folders = ['folder1', 'folder2']
query = Message.folder.test(lifter.lookups.value_in(sent_folders))

Also, as in is a registered keyword in python, for internal reasons, so that's why the lookup is named value_in instead of simply in.

The API is really not great here sorry :/

@n3storm
Copy link
Author

n3storm commented Sep 7, 2017

Thank you!

I am thinking how to add this information improved with extra expressions like contains etc to the docs. Could you please keep this issue open meanwhile?

@agateblue
Copy link
Owner

Sure, that would be a great addition. Feel free to provide a PR whenever you have the time :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants