django过滤检查条件

时间:2011-04-26 07:28:41

标签: django

这里我正在使用

age_from:-------    age to:--------------    //here ---- is text field
date from:------     dat_to:-------------

search button // this is my search button.

这里我匹配条件,如果从中选择年龄将搜索基于日期(从date到date)的年龄更大的年龄 类似于其余案例,如下面的代码所述:

现在我的问题是:

gender : o male o female
age_from:-------    age to:--------------
date from:------     dat_to:-------------

现在我怎么能写出性别部分,年龄部分和日期部分的条件..它有点令人困惑我正好......帮助我..

def search(request):

    age_from = request.POST["age_from"]
    age_to = request.POST["age_to"]
    date_from = request.POST["date_from"]
    date_to = request.POST["date_to"] 


    if age_from:
        age1 = 1
    if age_to:
        age2 = 1
    if date_from:
        date1 = 1
    if date_to:
        date2 = 1


    if age1 and not age2:
        if date1 and date2:
            patient = PatientInfo.objects.filter(age__gte=age_from , dateedit__range=(date_from,date_to))
        else:
            if date1:
                patient = PatientInfo.objects.filter(age__gte=age_from,dateedit__gte=date_from)
            else:
                if date2:
                    patient = PatientInfo.objects.filter(age__gte=age_from, dateedit__lte=date_to)
                else:
                    patient = PatientInfo.objects.filter(age__gte=age_from)

    if age2 and not age1:
        if date1 and date2:
            patient = PatientInfo.objects.filter(age__lte=age_to, dateedit__range=(date_from,date_to))
        else:
            if date1:
                patient = PatientInfo.objects.filter(age__lte=age_to, dateedit__gte=date_from)
            else:
                if date2:
                    patient = PatientInfo.objects.filter(age__lte=age_to, dateedit__lte=date_to)
                else:
                    patient = PatientInfo.objects.filter(age__lte=age_to)

    if age1 and age2:
        if date1 and date2:
            patient = PatientInfo.objects.filter(age__range=(age_from,age_to),dateedit__range=(date_from,date_to))
        else:
            if date1:
                patient = PatientInfo.objects.filter(age__range=(age_from,age_to),dateedit__gte=date_from)
            else:
                if date2:
                    patient = PatientInfo.objects.filter(age__range=(age_from,age_to),dateedit__lte=date_to)
                else:
                    patient = PatientInfo.objects.filter(age__range=(age_from,age_to))


    if date1 and not date2:
        if age1 and age2:
            patient = PatientInfo.objects.filter(dateedit__gte=date_from, age1__range=(age_from,age_to))
        else:
            if age1:
                patient = PatientInfo.objects.filter(dateedit__gte=date_from, age__gte=age_from)
            else:
                if age2:
                    patient = PatientInfo.objects.filter(dateedit__gte=date_from, age__lte=age_to)
                else:
                    patient = PatientInfo.objects.filter(dateedit__gte=date_from)

    if date2 and not date1:
        if age1 and age2:
            patient = PatientInfo.objects.filter(dateedit__lte=date_to, age1__range=(age_from,age_to))
        else:
            if age1:
                patient = PatientInfo.objects.filter(dateedit__lte=date_to, age__gte=age_from)
            else:
                if age2:
                    patient = PatientInfo.objects.filter(dateedit__lte=date_to, age__lte=age_to)
                else:
                    patient = PatientInfo.objects.filter(dateedit__lte=date_to)

    if date1 and date2:
        if age1 and age2:
            patient = PatientInfo.objects.filter(dateedit__range=(date_from,date_to), age1__range=(age_from,age_to))
        else:
            if age1:
                patient = PatientInfo.objects.filter(dateedit__range=(date_from,date_to),age__gte=age_from)
            else:
                if age2:
                    patient = PatientInfo.objects.filter(dateedit__range=(date_from,date_to), age__lte=age_to)
                else:
                    patient = PatientInfo.objects.filter(dateedit__range=(date_from,date_to))

以上代码适用于年龄和日期部分......工作正常.... 但当有三个条件时,它会让人感到困惑..没有得到

请提前填写性别,年龄和日期部分的代码......如上所述...

1 个答案:

答案 0 :(得分:3)

我建议您根据需要添加过滤条件,而不是像您一样尝试分叉代码。这是一个例子,你如何做到这一点:

patient = PatientInfo.objects.all(); # getting all patients (don't worry, query is not executed yet)

if age_from:
    patient = patient.filter(age__gte=age_from)

if age_to:
    patient = patient.filter(dateedit__lte=date_to)

if gender:
    patient = patient.filter(gender=gender)

# ...and so on

条件将相互添加,当您尝试重新引用结果时,将执行实际查询。 django不酷吗?