2014-11-03 3 views
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') 
+0

Возможно, некоторые из ваших предложений 'if' и условные ** m **? Помните ** DRY ** (** D ** не ** ** ** ** ** ** **). – Anzel

ответ

2

Для упрощения использования вы можете использовать args и kwargs. Я не очень внимательно прочитал ваши коды, вот пример для замены партий if-else:

args = [] 
kwargs = { 
    'landproperty__sownerid__id': get_id, 
    'geom__distance_lte': (pnt, D(km=kmdistance)), 
    ...... 
} 
if mysection != 'All': 
    kwargs['ssectionid__id'] = mysection 
if getvalue != '0': 
    args.append(Q(geom__within=SoilType.objects.get(id=getvalue).geom)) 
...... 
m = ButuanMaps.objects.filter(*args, **kwargs).values_list('clandpin') 
return HttpResponse(json.dumps(list(m)), content_type='application/json') 
Смежные вопросы