Jin Nii Sama
Jin Nii Sama

Reputation: 747

Django save data from calling api in views

So i have a views which have a Get and Post request that call to another django project API. But i want to also save the information that i get from the api call.

Project 1 has a Appointment table which has these field clinic Id, time, and queueNo. When i do a Post request to Project 2 to make/create an appointment, when successfully created, it will display those 3 field which i want to save into Project 1 Appointment table database. How do i do it ? My Appointment also has a API made so how do i save it there?

Here is the code for my view to call api to another django project

views.py

@csrf_exempt
def my_django_view(request):
    if request.method == 'POST':
        r = requests.post('http://127.0.0.1:8000/api/test/', data=request.POST)
    else:
        r = requests.get('http://127.0.0.1:8000/api/test/', data=request.GET)
    if r.status_code == 200:
        # r.text, r.content, r.url, r.json
        return HttpResponse(r.text)
    return HttpResponse('Could not save data')

Upvotes: 1

Views: 6479

Answers (2)

Thomas Jiang
Thomas Jiang

Reputation: 1323

Assuming your endpoint in Project 2 returns a JSON response with the fields that you need:

{
    "clinicId": 1,
    "time": some-time-string,
    "queueNo": 2
}

You can retrieve the response after making the request by calling r.json().

Based on this you can then treat r.json() as a dictionary and create the instance using Appointment.objects.create(**r.json()). Here's how it might look.

@csrf_exempt
def my_django_view(request):
    if request.method == 'POST':
        r = requests.post('http://127.0.0.1:8000/api/test/', data=request.POST)
    else:
        r = requests.get('http://127.0.0.1:8000/api/test/', data=request.GET)

    if r.status_code == 200 and request.method == 'POST':
        # Convert response into dictionary and create Appointment
        data = r.json()
        # Construct required dictionary
        appointment_attrs = {
            "clinicId": data["some-key-that-points-to-clinicid"],
            "time": data["some-key-that-points-to-time"],
            "queueNo": data["some-key-that-points-to-queue-num"]
        }
        appointment = Appointment.objects.create(**appointment_attrs)
        # r.text, r.content, r.url, r.json
        return HttpResponse(r.text)
    elif r.status_code == 200:  # GET response
        return HttpResponse(r.text)

    return HttpResponse('Could not save data')

Upvotes: 4

Charles Langlois
Charles Langlois

Reputation: 4298

You first have to extract the data from the responses. If you're using the requests library and the API you're calling is responding in JSON, you can do something like data = r.json().

I don't know the structure of your second API's responses, though, but I assume you can then get your fields from the data object.

Then it's a matter of saving what you want with whatever database interface you're using. If it's the django ORM, and you have an Appointement model somewhere, you can do something like

Appointment(
    clinic_id=data["clinic_id"], 
    time=data["time"], 
    queueNo=data["queueNo"]
).save()

and you're done...

Upvotes: 1

Related Questions