1👍
✅
For example you can do
from django.db.models import Q
def search(request):
if request.method == "GET":
search_terms = request.GET['title']
search_terms = search_terms.split(',')
#get rid of duplicates
search_terms = set(search_terms)
queryargs = [Q(title__contains=i) for i in search_terms]
jobs = Job.objects.filter(*queryargs)
1👍
Use django.db.models.Q
objects. Like this:
from django.db.models import Q
def search(request):
if request.method == "GET":
search_terms = request.GET['title']
search_filter = Q()
for term in search_terms.split(','):
search_filter |= Q(title__contains=term)
jobs = Job.objects.filter(search_filter)
This will search for any of the terms (OR), to search for all terms (AND) use search_filter &= Q(title__contains=term)
instead.
- [Answered ]-Django Rest Framework – use serializers to send json with requests library
- [Answered ]-Goose-extractor for python 3.4 Error
- [Answered ]-Django Transaction did not work
- [Answered ]-Understanding where to put the IPN reciever function in django-paypal
- [Answered ]-Parent calling abstract method in python
Source:stackexchange.com