在django中将数据添加到我的Json

时间:2018-02-10 18:50:42

标签: json django

这就是我的JSON数据的样子

{"id": 50, "first_digit": "2", "second_digit": "1", "calculate": "Addition"}

我想将额外数据"result":"3"添加到我的JSON

  

这是我的观点

def calc(request):
if request.method == 'POST':
    first_number = request.POST['first number']
    second_number = request.POST['second number']
    operation = request.POST['operation']

    result = do_calc(operation, first_number, second_number)
                                # how to pass the result to my tempelate
    value = Calculation.objects.create(
        first_digit=first_number,
        second_digit=second_number,
        calculate=operation
    )
    data = model_to_dict(value)

    return JsonResponse(data)

任何人都可以帮助我吗

1 个答案:

答案 0 :(得分:2)

据推测,model_to_dict()的输出是一个字典。因此,您只需将您的值添加到该字典:

data = model_to_dict(value)
data['result'] = result
return JsonResponse(data)