Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
Looping over JSONResponse() result from django in jquery

I am interested in getting the results of an insert operation from views.py in form a JSON. I am getting the results alright, I think. my view looks like this:

added={}
if request.method=='POST':
        #Post method initiated.

        try:
            for id in allergies:
                   allergy=PatientAllergy(patient=patient,allergy_id=id,addedby=request.user)
               allergy.save()
               added[allergy.id]=id
        except BaseException as e:
            pass

return JsonResponse(added,safe=False)

The records, passed from JQUERY, added successfully to the database. What I want to get now is a JSON result in the form of {12:1, 13:2}.

My firebag shows the response as:

    12:1
    13:2

I am not sure if this a valid JSON or not. If I do list(added), it gives instead:

    0: 12
    1: 13

which I don't want. The problem I am having now I want to go thru the returned items but I am getting incorrect results. I basically want to get 12:1, 13:2.

         $.ajax({
            type: "POST",
            url: "/patient/addallergy/",
            data: postForm,
            dataType : "json",
            cache: "false",
            success: function (result) {

                    alert(result.length); //gives undefined, rendering the below line meaningless

                    if (result.length>0){


                        $.each(result,function(key,value){
                            alert(key);
                            alert(value);

                           });

                    }


            },
            fail: function (result){

            }


        }); 
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

change your views like this.

added_list=[]
    if request.method=='POST':
            #Post method initiated.

            try:
                for id in allergies:
                   added ={}                  allergy=PatientAllergy(patient=patient,allergy_id=id,addedby=request.user)
                   allergy.save()
                   added[allergy.id]=id
                   added_list.append(added)
            except BaseException as e:
                pass

    return JsonResponse(added_list,safe=False)

and in jquery

$.ajax({
        type: "POST",
        url: "/patient/addallergy/",
        data: postForm,
        dataType : "json",
        cache: "false",
        success: function (result) {

                alert(result.length); //gives undefined, rendering the below line meaningless

                if (result.length>0){

                     alert(JSON.stringify(result))
                    $.each(result,function(index,value){
                        console.log(value);



                       });

                     result.forEach( function (eachObj){
                       for (var key in eachObj) {
                                  alert(key);
                             alert(eachObj[key])
                            }
                       });

                }


        },
        fail: function (result){

        }


    });
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda