How to send an object with other variables back to the client using json serialize?

How to add dict a to response and how can I get two objects at the ajax?

view

def abc(request):
    cp = Cp.objects.get(id=1)
    cp = serializers.serialize('json', [cp,])
    cp = json.loads(cp)

    a = {'a': 'a', 'b': 'b'}

    return HttpResponse(data)

js

$.ajax({
    //
    success: function(data){
    }    
})

1 answer

  • answered 2019-07-18 15:10 Дмитрий Дмитрук

    Thnx @WillemVanOnsem for help

    views

    def abc(request):
        cp = serializers.serialize('json', [cp,])
        cp = json.loads(cp)
        cp = json.dumps(cp)
        other = {'rate': float(rate), 'eair': float(eair), 'overpayment': overpayment, 'total_payment': round(total_payment), 'monthly_payment':round(monthly_payment)}
        return JsonResponse({'cp': cp, 'other': other})
    

    js

      $.ajax({
        //
        success: function(data) {
          var cp_json = JSON.parse(data.cp);
          var other = data.other;
          var credit_payment = cp_json[0].fields;
          $('.rate').html(credit_payment.rate + '%')
        }
      })