134👍
import operator
from django.db.models import Q
User.objects.filter(reduce(operator.and_, (Q(first_name__contains=x) for x in ['x', 'y', 'z'])))
for python 3
from functools import reduce
.
39👍
import operator
from django.db.models import Q
q = ['x', 'y', 'z']
query = reduce(operator.and_, (Q(first_name__contains = item) for item in q))
result = User.objects.filter(query)
- [Django]-Set up a scheduled job?
- [Django]-Disable migrations when running unit tests in Django 1.7
- [Django]-RuntimeWarning: DateTimeField received a naive datetime
5👍
More readable solution.
qs = User.objects.all()
for search_term in ('x', 'y', 'z'):
qs = qs.filter(first_name__contains=search_term)
Note: Querysets are lazy, so this code makes 1 DB query.
- [Django]-Name '_' is not defined
- [Django]-How to set up custom middleware in Django?
- [Django]-Use Python standard logging in Celery
1👍
from django.db.models import Q
User.objects.filter(Q(first_name__contains=x)&Q(first_name__contains=y)&Q(first_name__contains=z))
Works for me on Django 1.8 python 2.7
doc => https://docs.djangoproject.com/en/1.8/ref/models/querysets/#q-objects
for more recent =>
https://docs.djangoproject.com/en/2.1/ref/models/querysets/#q-objects
- [Django]-Readonly models in Django admin interface?
- [Django]-In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?
- [Django]-How to combine django "prefetch_related" and "values" methods?
1👍
The accepted solution didn’t work for me, but this did:
list = ['x', 'y', 'z']
results = User.objects.filter(first_name__contains=list[0])
del list[0]
for l in list:
results = results.filter(first_name__contains=l)
The first results variable will store a list of all the objects with the first_name name field value ‘x’.
And then in the for loop you filter for ‘y’ amongst the first filter results. Now you have a QuerySet that should contain a list of items where both ‘x’ and ‘y’ can be found. Now amongst those you filter for any item that contains ‘z’ as well.
This should work for any length list.
- [Django]-Django auto_now and auto_now_add
- [Django]-Django models: Only permit one entry in a model?
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django
1👍
the_list= []
data_list= ['x', 'y', 'z']
for i in data_list:
a = User.objects.filter(first_name__contains=project).values('etc')
the_list+= a
- [Django]-Why does django run everything twice?
- [Django]-Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?'
- [Django]-How do I run tests against a Django data migration?
0👍
This worked for me in django 2.2, python 3.8, using lambda instead of ‘operator’.
Explanations on lambda can be found here: https://www.python-course.eu/lambda.php
from functools import reduce
from django.db.models import Q
my_list = ['x','y','z']
User.objects.filter(reduce(lambda x, y: x & y, [Q(first_name__contains= i for i in my_list]))
- [Django]-Django url tag multiple parameters
- [Django]-How to pass django rest framework response to html?
- [Django]-Gunicorn, no module named 'myproject
0👍
It doesn’t apply exactly here, but, if you have a ManyToManyField
and you want to check if it contains a specific value, check this https://www.revsys.com/tidbits/tips-using-djangos-manytomanyfield/.
I have a "Products" and Django native "Users" model. My "Products" model has a many-to-many field users
pointing to "Users". Then, I wanted to check if this list-like field contained the logged in user. I did that by ...users__username_icontains=request.user.username...
and the link above helped me to understand better what is a many-to-many field and how it works.
- [Django]-How to run this code in django template
- [Django]-Django template how to look up a dictionary value with a variable
- [Django]-Best practices for adding .gitignore file for Python projects?