1

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')
3
  • Where is your data storage interface? Are you using django ORM? Commented Jan 24, 2018 at 3:20
  • Django ORM ? from what i know, all im using is just django and django rest framework Commented Jan 24, 2018 at 3:27
  • 1
    How did you define your Appointment table? Did you do it in Python, or directly in SQL? Commented Jan 24, 2018 at 3:28

2 Answers 2

4

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')
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks ! But sorry to trouble you more, what if the json i get back have 5 fields(a,b,c,d,e) but i only want to save these 3 fields(a,c,e). What do i do then ? And also if the field name in the json i get back has different name than the field i am saving into, how do i save it ?
I updated my answer - it's more of a matter of transforming a Python dictionary. So you can use any method, what I've demonstrated above is just one of the ways you can do it - by creating a dictionary with the keys that you need and mapping the data from the data dictionary over to the new dictionary. ** is a way of unpacking Python dictionaries, saves you time from having to type the individual fields out. If this is new to you, you may find more information via Google.
Just to add on, in this case Appointment.objects.create(**appointment_attrs) translates to Appointment.objects.create(clinicId=<somevalue>, time=<sometime>, queueNo=<somenum>)
So its like this "clinicId": data["clinicId"], ? Alright will go and try it out. Thank you
If the they have the same keys then I don't think you need to do that. All you need to do is remove the keys that you don't need from data dictionary by using something like del data["key-that-you-dont-want"] and do .create(**data).
|
1

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...

2 Comments

I create the table in models.py. Django project 1 is these 3 field on top as stated. clinic Id, time, and queueNo. Django project 2 also has the same table with these 3 field as well as more additional field. I never heard of django ORM but thanks. All i have done for tables is models.py serializer.py and views.py
"ORM" means "Object-relational mapping". It's how you define and interact with relational database(i.e. SQL) tables in a object oriented way. When you define models and make queries or save data to the database using the django framework, you're using the django ORM. There are others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.