Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django filter from two fields with single input

Tags:

django

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..

like image 285
user2251503 Avatar asked Jan 17 '26 18:01

user2251503


1 Answers

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)
like image 111
Pavel Anossov Avatar answered Jan 20 '26 10:01

Pavel Anossov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!