Anand
Anand

Reputation: 139

add data to my Json in django

This is how my JSON data looks like

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

I want to add extra data "result":"3" to my JSON

this is my view

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)

Can anyone help me with it

Upvotes: 0

Views: 42

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

Presumably the output of model_to_dict() is a dict. So you can simply add your value to that dict:

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

Upvotes: 2

Related Questions