开发者

How to refine an initial query in Django?

Following the reply to this question (Thanks again Ellie P!) I created a sea开发者_Go百科rch page and a results page.

For instance if you search for the lawyer "delelle" the result page shows her firm, school and year graduated. But instead of displaying her info, I want to display other lawyers who graduated from the same school the same year.

This is the view:

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        lawyers = Lawyer.objects.filter(last__icontains=q)
        return render_to_response('search_results.html', {'lawyers': lawyers, 'query': q})
    else:
        return HttpResponse('Please submit a search term.')

Can anyone help me understand how I can save the school and year_graduated from the initial query and do a school and year_graduated search and display that result?

Thank you!

Edit

Model is here

Edit2

I tried a few things from the QuerySet API

Given the search query is "akira":

>>> akira_year = Lawyer.objects.filter(first__icontains="Akira").values_list('year_graduated').order_by('year_graduated')

>>> print akira_year
[(u'2000',)]

But

>>> Lawyer.objects.filter(year_graduated__icontains=akira_year[0])
[]

doesn't work.

Can anyone help with the correct syntax to use in this case?

Thanks


This view function answers the question:

def search(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
        q_school = Lawyer.objects.filter(last__icontains=q).values_list('school', flat=True)
        q_year = Lawyer.objects.filter(last__icontains=q).values_list('year_graduated', flat=True)
        lawyers = Lawyer.objects.filter(school__icontains=q_school[0]).filter(year_graduated__icontains=q_year[0])        
        return render_to_response('search_results.html', {'lawyers': lawyers, 'query': q})
    else:
        return HttpResponse('Please submit a search term.')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜