I have a database where I have users stored. Those users have a 'phone_number' property.
I want to retrieve a list of users whose 'phone_number' is contained in a list I have.
Something like:
contacts = ["123","321","456","654"]
friends = []
for user in User.select(User.phone_number).where(User.phone_number in contacts):
friends.append(user.phone_number)
Issue: That piece of code returns every user's phone number and not just the ones contained in contacts.
I can achieve what I want with:
friends = [u.phone_number for u in User.select(User.phone_number) if u.phone_number in contacts]
but as far as I understand this approach is probably retrieving all users from the database first, turning them into python objects and then doing the filtering. It seems incredibly inefficient if I have a large number of users since it's python doing the filtering and not the database.
This documentation shows the query operators for peewee: http://peewee.readthedocs.org/en/latest/peewee/querying.html#query-operators
To answer your question specifically you can do
User.phone_number << contacts
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.