English 中文(简体)
Django Query 内容提要
原标题:Django QuerySet API: How do I join iexact and icontains?

我加入:

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

http://swims with.com/search-form/"rel=“nofollow noreferer”> 该网站

如果你尝试最后名称:阿巴斯和第一名称:Amr告诉你,友好堡有1名校友。

但是,如果你只尝试第一名字,那么它就说,在称为“友好”的数据库中没有律师(显然有律师)。

如果I 更改(last__iexact=last_name)(last__icontains=last_name) 之后,找到了最后姓名空白的工件。

但是,有<条码> 最终__icontains=last_name。 如果你寻找“col子”,你也获得“col子”和“wood材”,这不是我想要的东西。

你们是否知道我如何使用<代码>iexact,如果是空白,是否也忽视了这一条?

增 编

这是观点功能:

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, })
最佳回答
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签