简体   繁体   English

编写此功能的更有效方法? (Django,AJAX)

[英]More efficient way of writing this function? (Django, AJAX)

The way I have written the function works now, I'm just wondering if there is a better or more efficient way to write it. 我编写函数的方式现在可以运行,我只是想知道是否有更好或更有效的方法来编写它。 It is an ajax call that gets a list for further filtering a geography, for example if county is chosen then it will get a list of all the counties in that state. 这是一个ajax调用,它获取用于进一步过滤地理位置的列表,例如,如果选择了县,那么它将获得该州所有县的列表。 I'm still learning coding and want to get better at using good coding practices. 我仍在学习编码,并希望更好地使用良好的编码实践。

$.ajax({
        url : "filters",
        type : "POST",
        data : { search : selected_state, geog : selected_geog },
        success: function(data) {
            $.each(data, function(i, v) {
              switch(selected_geog) {
                case 'County':
                case 'Dma':
                  filters.push(v.fields.name);
                  break;
                case 'Zip':
                  var exists = $.inArray(v.fields.zcta, data_check);
                  if (exists < 0) {
                    data_check.push(v.fields.zcta);
                  }
                  data_check.sort();
                  var exists2 = $.inArray(v.fields.county, filters);
                  if (exists2 < 0) {
                    filters.push(v.fields.county);
                  }
                  filters.sort();
                  break;
                case 'CD':
                  filters.push(v.fields.cd114fp);
                  break;
                case 'Zones':
                  var exists = $.inArray(v.fields.p_dma, filters);
                  if (exists < 0) {
                    filters.push(v.fields.p_dma);
                  }
                  filters.sort();
                  var exists2 = $.inArray(v.fields.owner, attrfilters);
                  if (exists2 < 0) {
                    attrfilters.push(v.fields.owner);
                  }
                  SHPconverter.attrfilters.sort();
                  var exists3 = $.inArray(v.fields.scode, data_check);
                  if (exists3 < 0) {
                    data_check.push(v.fields.scode);
                  }
                  data_check.sort();
                  break;
              }
            })
        }
      })

and here is the django view 这是django视图

def filters(request):
    searchVar = request.POST.getlist('search[]')
    geogVar = request.POST.get('geog')

    if geogVar == 'County':
        county_as_json = serialize(
            'json', County.objects.filter(
            state__in=searchVar).order_by('state', 'name'))
        return HttpResponse(county_as_json, content_type='json')

    elif geogVar == 'DMA':
        dma_as_json = serialize(
            'json', Dma.objects.filter(
            state__in=searchVar).order_by('state', 'name'))
        return HttpResponse(dma_as_json, content_type='json')

    elif geogVar == 'CD':
        cd_as_json = serialize(
            'json', Cd.objects.filter(
            state__in=searchVar).order_by('state', 'cd'))
        return HttpResponse(cd_as_json, content_type='json')

    elif geogVar == 'Zip':
        zip_as_json = serialize(
            'json', Zip.objects.filter(
            state__in=searchVar).order_by('state', 'county'))
        return HttpResponse(zip_as_json, content_type='json')

    elif geogVar == 'Zones':
        zones_as_json = serialize(
            'json',  Zones.objects.filter(
            state__in=searchVar).order_by('sname'))
        return HttpResponse(softzones_as_json, content_type='json')

You can reduce the entire if statement section by creating a dictionary lookup with a tuple as values 您可以通过创建一个以元组为值的字典查找来减少整个if语句部分

geo_lookup = {'County': (County, ['state', 'name']),
                  'DMA': (Dma, ['state', 'name']),
                  'CD': (Cd, ['state', 'cd']),
                  'Zip': (Zip, ['state', 'county']),  
                  'Zones': (Zones, ['sname']),
                  }

lookup_model = geo_lookup[geogVar][0]   
ordering = geo_lookup[geogVar][1]
as_json = serialize('json', lookup_model.objects.filter(state__in=searchVar).order_by(*ordering))
return HttpResponse(as_json, content_type='json')

You also seem to be trying to serialize models, something which Django Rest Framework specializes in, so just using that instead would stop the requirement for you to manually do all this in a view. 您似乎还试图对模型进行序列化,这是Django Rest Framework擅长的事情,因此仅使用该模型将停止您在视图中手动执行所有这些操作的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM