简化Django中的条件

时间:2014-11-03 01:18:41

标签: python django

我在下面有这个代码,其中包含许多条件。我用它进行搜索过滤,有没有简化它的机会?在搜索中添加另一个字段时发现它很长。

目前,我有5个字段用于搜索:kmdistancemysectiongetareagetvaluegetvalue

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

1 个答案:

答案 0 :(得分: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')