-1

我有一个目录,我有很多重复的寄存器,我只需要得到一个寄存器,因为我会做一些嵌套的 DropDownLists,所以我只需要其中一个值。我不知道该怎么做,我使用 Django 和 ajax 调用从数据库中获取数据。

视图.py

def adendumSeach(request):
    if request.POST.has_key('grupname') and request.is_ajax():
        x = request.POST['grupname']
        y = adendum.objects.filter(adendum_credencial=x)
        formulario = serializers.serialize('json', y)
        return HttpResponse(formulario, mimetype="application/json")
    else:
        return render_to_response('ExpedienteDetalle.html',
        context_instance=RequestContext(request))

ajax.js

function adendum(idgrupo) {
      $.ajax({
            url: "/adendum_search",
            type: "POST",
            dataType: "json",
            data: {
                grupname: idgrupo,
                csrfmiddlewaretoken: '{{ csrf_token }}'
            },
                 success: function (json) {
                var jsonResponse = eval(json);
                $.each(jsonResponse, function(index, element){
                    $("#aden").css("display", "block");
                    $("#thconsultas").css("display", "block");
            }); ;
                 },
             error: function (xhr, errmsg, err) {
                alert(xhr.status + "  Inside error :    " + xhr.responseText);
            }
        });
     }
4

1 回答 1

1

has_key已弃用。

x并且y是错误的变量名称。

adendum类必须是大写

正如@oleg 所说,使用distinct

def adendumSeach(request):
    if 'grupname' in request.POST  and request.is_ajax():
        grupname = request.POST['grupname']
        adendum_list = adendum.objects.filter(adendum_credencial=grupname).distinct()
        formulario = serializers.serialize('json', adendum_list)
        return HttpResponse(formulario, mimetype="application/json")
    else:
        return render_to_response('ExpedienteDetalle.html',
        context_instance=RequestContext(request))
于 2013-10-26T19:18:50.007 回答