here im using
age_from:------- age to:-------------- //here ---- is text field
date from:------ dat_to:-------------
search button // this is my search button.
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:
now my problem is :
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)
开发者_Go百科 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
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
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?
精彩评论