django查询过滤条件

时间:2011-04-26 08:44:25

标签: django

  

可能重复:
  django filter check conditions

这里我正在使用

age_from:------- age to:-------------- //这里----是文本字段 日期来自:------ dat_to:-------------

搜索按钮//这是我的搜索按钮。

在这里,我将条件与agefrom,ageto,date from date_to匹配,一旦您使用以下代码,请将其匹配。

现在我的问题是:(我想要这3个部分的确切:) 性别:o男性o女性 age_from:-------年龄:-------------- 日期来自:------ dat_to:-------------

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

并使用该模型:

class PatientInfo(models.Model):  age = models.IntegerField() gender = models.CharField(max_length = 200) dateedit = models.DateTimeField()

def搜索(请求):

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))

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

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

0 个答案:

没有答案