0
У меня есть этот код ниже, который состоит из множества условий. Я использовал его для фильтрации поиска, любой шанс упростить его? Я нахожу это долго, добавляя другое поле в поиске.Упрощение условия в Django
Прямо сейчас, у меня есть 5 полей, используемых в поиске, а именно: kmdistance
, mysection
, getarea
, getvalue
и getvalue
.
def section_landpins(request):
if request.method == "GET":
get_id = request.user.id
pnt = ButuanMaps.objects.get(clandpin='162-03-0001-017-33').geom
kmdistance = request.GET.get('kmtocity', default=100)
mysection = request.GET.get('mysection', default='All')
getarea = request.GET.get('getarea', default=5500000)
getvalue = request.GET.get('mysoiltype', default=1)
getvalue1 = request.GET.get('myerosion', default=1)
reference = None
reference1 = None
if mysection == 'All' and getvalue == '0' and getvalue1 == '0':
m = ButuanMaps.objects.filter(landproperty__sownerid__id=get_id,
geom__distance_lte=(pnt, D(km=kmdistance)),
narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
elif mysection != 'All' and getvalue == '0' and getvalue1 == '0':
m = ButuanMaps.objects.filter(landproperty__sownerid__id=get_id,
geom__distance_lte=(pnt, D(km=kmdistance)),
ssectionid__id=mysection,
narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
elif mysection == 'All' and getvalue != '0' and getvalue1 == '0':
reference = SoilType.objects.get(id=getvalue).geom
m = ButuanMaps.objects.filter(Q(geom__within=reference), landproperty__sownerid__id=get_id, geom__distance_lte=(pnt, D(km=kmdistance)), narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
elif mysection == 'All' and getvalue == '0' and getvalue1 != '0':
reference1 = ErosionMap.objects.get(id=getvalue1).geom
m = ButuanMaps.objects.filter(Q(geom__within=reference1), landproperty__sownerid__id=get_id, geom__distance_lte=(pnt, D(km=kmdistance)), narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
elif mysection != 'All' and getvalue == '0' and getvalue1 != '0':
reference1 = ErosionMap.objects.get(id=getvalue1).geom
m = ButuanMaps.objects.filter(Q(geom__within=reference1),
landproperty__sownerid__id=get_id,
geom__distance_lte=(pnt, D(km=kmdistance)),
ssectionid__id=mysection, narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
elif mysection != 'All' and getvalue != '0' and getvalue1 == '0':
reference = SoilType.objects.get(id=getvalue).geom
m = ButuanMaps.objects.filter(Q(geom__within=reference),
landproperty__sownerid__id=get_id,
geom__distance_lte=(pnt, D(km=kmdistance)),
ssectionid__id=mysection, narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
elif mysection != 'All' and getvalue != '0' and getvalue1 != '0':
reference = SoilType.objects.get(id=getvalue).geom
reference1 = ErosionMap.objects.get(id=getvalue1).geom
m = ButuanMaps.objects.filter(Q(geom__within=reference), Q(geom__within=reference1),
landproperty__sownerid__id=get_id,
geom__distance_lte=(pnt, D(km=kmdistance)),
ssectionid__id=mysection, narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
elif mysection == 'All' and getvalue != '0' and getvalue1 != '0':
reference = SoilType.objects.get(id=getvalue).geom
reference1 = ErosionMap.objects.get(id=getvalue1).geom
m = ButuanMaps.objects.filter(Q(geom__within=reference), Q(geom__within=reference1),
landproperty__sownerid__id=get_id,
geom__distance_lte=(pnt, D(km=kmdistance)),
narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
else:
reference = SoilType.objects.get(id=getvalue).geom
reference1 = ErosionMap.objects.get(id=getvalue1).geom
m = ButuanMaps.objects.filter(Q(geom__within=reference),
landproperty__sownerid__id=get_id,
geom__distance_lte=(pnt, D(km=kmdistance)),
ssectionid__id=mysection,narea__lte=getarea).values_list('clandpin')
return HttpResponse(json.dumps(list(m)), content_type='application/json')
Возможно, некоторые из ваших предложений 'if' и условные ** m **? Помните ** DRY ** (** D ** не ** ** ** ** ** ** **). – Anzel