0

I have a model like this:

class Info(models.Model):
    tape_id = models.TextField()
    name = models.TextField()
    video_type = models.TextField()
    date = models.DateTimeField()
    director = models.CharField(max_length=255)
    cameraman = models.CharField(max_length=255)
    editor = models.CharField(max_length=255)
    time_code = models.TextField()
    tag1 = models.TextField()

User can search from tape_id, name, director and cameraman using the same search input box. I have a search view like this:

 if request.method == 'POST':
        search_inp = request.POST['search_box']
        tape_id = Info.objects.filter(tape_id__exact=search_inp)
        res = Info.objects.filter(name__icontains=search_inp)
        res = Info.objects.filter(director__icontains=search_inp)
        res = Info.objects.filter(cameraman__icontains=search_inp)
        total_video = res.count()
        if len(res) == 0 and len(tape_id) == 0 :
            result = "No videos found!!"
            return render_to_response('no_results_only.html', {'result':result}, context_instance=RequestContext(request))
        else:
            date1 = [i.date for i in res]
            date = [i.strftime("%B %d, %Y") for i in date1]
            a = zip(res, date)
            return render_to_response('list_videos.html', {'a':a, 'total_video':total_video}, context_instance=RequestContext(request))
        return HttpResponseRedirect('/')

I thought it would work at first but it doesn't. First res variable can contain the value whereas the last res be empty which will return to the no_results.html. I want to deploy this search using the same input box. How can I make this work?

2
  • You know that you're rewriting res with each of these queries, right? Anyway, use Q for OR queries. Commented Apr 2, 2013 at 6:33
  • Yes, I realized that, I was completely unknown with Q. Thanks mate! Commented Apr 2, 2013 at 6:40

2 Answers 2

1

First you need to import Q to use for OR filtering:

from django.db.models import Q

Then your code should look like this:

if request.method == 'POST':
    search_inp = request.POST['search_box']
    res = Info.objects.filter(
        Q(tape_id__exact=search_inp) | 
        Q(name__icontains=search_inp) |
        Q(director__icontains=search_inp) |
        Q(cameraman__icontains=search_inp)
    )
    total_video = res.count()
    if len(res) == 0:
        result = "No videos found!!"
        return render_to_response('no_results_only.html', {'result':result}, context_instance=RequestContext(request))
    else:
        date1 = [i.date for i in res]
        date = [i.strftime("%B %d, %Y") for i in date1]
        a = zip(res, date)
        return render_to_response('list_videos.html', {'a':a, 'total_video':total_video}, context_instance=RequestContext(request))
    return HttpResponseRedirect('/')
Sign up to request clarification or add additional context in comments.

Comments

0

It is recommended to apply OR conditions in the filter. You can refer the documentation for the syntax.

Q(question__startswith='Who') | Q(question__startswith='What')

It will make sure that you will get an unique list of objects in the result.

1 Comment

In my case how can I do that? Q(Info.objects.filter(tape_id__exact=search_inp)) | ......... ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.