django过滤器检查条件 [英] django filter check conditions

查看:144
本文介绍了django过滤器检查条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里使用

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

search button // this is my search button.

这里我匹配条件,如果选择年龄从它将搜索更大的年龄基于日期(从日期到date_to)
与其他情况相同,如下面的代码所示:

here i match the conditions if select age from it will search the age which is greater on the base on date (date from to date_to) similarly for rest of the cases as mention in below code:

现在我的问题是:

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

现在我如何写出性别部分,年龄部分和日期部分的条件..它有点混乱我正在...帮助我..

now how can i write the condition for gender part ,age part and date part.. its bit confusing im getting exactly... help me..

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

上面的代码是年龄和日期部分...正在工作完美....
,但是当有三个条件时,它会令人困惑。没有得到

the above code is for age and date part... is working perfectly.... but when comes with three condition it confusing.. not getting

请将性别,年龄和日期部分的代码提前写为thanx。

please write the code for gender,age and date part.. as above thanx in advance...

推荐答案

我建议您添加过滤条件因为它们是必需的,而不是像你一样尝试分割你的代码。这是一个例子,你可以如何做:

I suggest you add filter conditions as they are needed, and not try to fork your code like you do. Here is an example, how you can do it:

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很酷吗?

Conditions will be added one to another and the actual query will be executed when you will try to refference the result. Isn't django cool?

这篇关于django过滤器检查条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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