Django QuerySet API:如何加入iexact和icontains? [英] Django QuerySet API: How do I join iexact and icontains?

查看:234
本文介绍了Django QuerySet API:如何加入iexact和icontains?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个加入:

lawyers = Lawyer.objects.filter(last__iexact=last_name).filter(first__icontains=first_name)

这是网站

如果你尝试姓氏:阿巴斯和名字:Amr它告诉你,amr abbas有1个同学。

If you try Last Name: Abbas and First Name: Amr it tells you that amr abbas has 1 schoolmates.

但是,如果您尝试使用名字,则表示数据库中没有律师(显然是)。

But if you try First name only it says that there are no lawyers in the database called amr (obviously there is).

如果我将(last__iexact = last_name)更改为(last__icontains = last_name)然后将姓氏留空

If I change (last__iexact=last_name) to (last__icontains=last_name) then leaving Last Name blank works fine and amr is found.

但是,如果您搜索collin,则可以使用 last__icontains = last_name collins和collingwood这不是我想要的。

But with last__icontains=last_name if you search for "collin" you also get "collins" and "collingwood" which is not what I want.

你知道我如何使用 iexact 如果空白的话也会被忽略?

Do you know how I can use iexact and also have it ignored if it is blank?

谢谢

这是查看功能

def search_form(request):
    if request.method == 'POST':
        search_form = SearchForm(request.POST)
        if search_form.is_valid():
            last_name = search_form.cleaned_data['last_name']
            first_name = search_form.cleaned_data['first_name']
            lawyers = Lawyer.objects.filter(last__iexact=last_name).filter(first__icontains=first_name)
            if len(lawyers)==0:
                form = SearchForm()
                return render_to_response('not_in_database.html', {'last': last_name, 'first': first_name, 'form': form})
            if len(lawyers)>1:
                form = SearchForm(initial={'last_name': last_name})
                return render_to_response('more_than_1_match.html', {'lawyers': lawyers, 'last': last_name, 'first': first_name, 'form': form}) 
            q_school = Lawyer.objects.filter(last__icontains=last_name).filter(first__icontains=first_name).values_list('school', flat=True)
            q_year = Lawyer.objects.filter(last__icontains=last_name).filter(first__icontains=first_name).values_list('year_graduated', flat=True)
            lawyers1 = Lawyer.objects.filter(school__iexact=q_school[0]).filter(year_graduated__icontains=q_year[0]).exclude(last__icontains=last_name)
            form = SearchForm()
            return render_to_response('search_results.html', {'lawyers': lawyers1, 'last': last_name, 'first': first_name, 'form': form})
    else:
        form = SearchForm()
        return render_to_response('search_form.html', {'form': form, })


推荐答案

您不必一次构建QuerySet。

You don't have to build the QuerySet all in one go.

lawyers = Lawyer.objects.all()
if last_name:
    lawyers = lawyers.filter(last__iexact=last_name)
if first_name:
    lawyers = lawyers.filter(first__icontains=first_name)

Django将不会评估QuerySet,直到它需要(在这种情况下,len()调用强制它来评估),所以你可以保持堆叠过滤器的所有日子,直到你准备好运行查询。

Django won't evaluate the QuerySet until it needs to (in this case, the len() call forces it to evaluate), so you can keep stacking filters on all days long until you're ready to run the query.

http://docs.djangoproject.com/en/dev/ref/models/querysets/#when-querysets-are-evaluated

此外,您不需要在以后创建新的QuerySets,您只需使用现有的。

Additionally, you don't need to create new QuerySets later on, you can just use the existing one.

q_school = Lawyer.objects.filter(last__icontains=last_name).filter(first__icontains=first_name).values_list('school', flat=True)
q_year = Lawyer.objects.filter(last__icontains=last_name).filter(first__icontains=first_name).values_list('year_graduated', flat=True)

可以是:

q_school = lawyers.values_list('school', flat=True)
q_year = lawyers.values_list('year_graduated', flat=True)

这篇关于Django QuerySet API:如何加入iexact和icontains?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆