agateblue / lifter

A generic query engine, inspired by Django ORM
ISC License
449 stars 16 forks source link

How to query value in list? #44

Open n3storm opened 7 years ago

n3storm commented 7 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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