简体   繁体   中英

django filter from two fields with single input

I'm having table with fields skills and title. I want to filter these fields with single input.

models.py
class job(model.Model):
     title = models.CharField(max_length=30)
     skills = models.CharField(max_length=30)

search.html
<form>
<input type="text" name="skills" />
<input type="submit" />
</form>

views.py
jobs.objects.filter(skills__icontains=request.GET['skills'],title__icontains=request.GET['skills'])

while i trying this code can't get output. I need output when entering title or skills in input box it matches with both skills and title. it should be a single input.some one help me..

Your code searches for jobs where BOTH title AND skills contain request.GET['skills'] . To search for jobs where title OR skills (or both) contain request.GET['skills'] , you have to use Q objects:

from django.db.models import Q
...
search = request.GET['skills']
        #     skills contain search  OR  title contains search
query = Q(skills__icontains=search ) | Q(title__icontains=search )
job.objects.filter(query)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM