Jin Nii Sama
Jin Nii Sama

Reputation: 747

Django save and enter data after API call in views

I have 2 django project, D1 and D2. D1 has a table call T1 and D2 has a table call T2.

So i have a view in D1 that do api call to the api in D2 and save the value that i get after a POST request into T1. But i also want to be able to enter data in other field of table T1.

example: t2 only has book field, t1 has book and author field. When D1 do a post method to t2 in D2, it will return a book value which will be save into t1 but i also want the user to enter the author themself. How do i do it ?

Here is my code

models.py

In D1:

class T1(models.Model):
    book = models.CharField(max_length=10, blank=True, null=True)
    author = models.CharField(max_length=10, blank=True, null=True)

In D2

class T2(models.Model):
    book = models.CharField(max_length=10, blank=True, null=True)

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 == 201 and request.method == 'POST':
        data = r.json()
        testsave_attrs = {
            "book": data["book"],
        }
        testsave= T1.objects.create(**testsave_attrs)
        return HttpResponse(r.text)
    elif r.status_code == 200:  # GET response
        return HttpResponse(r.json())
    else:
        return HttpResponse('Could not save data')

Upvotes: 0

Views: 2224

Answers (1)

Thomas Jiang
Thomas Jiang

Reputation: 1323

I believe you can simply do it in the same POST request made to my_django_view. You can treat request.POST as a dictionary. It basically has key-value pairs of all the user inputs in the request and maybe stuff like your CSRF token. In any case, in your front-end, you'd want to have a form or something to send, together with your other existing request.POST data such that you can extract it out in your view.

@csrf_exempt
def my_django_view(request):

    author = None  # let author be None at the start

    if request.method == 'POST':
        post_data = request.POST.copy()  # make a copy
        author = post_data.dict().pop("author", None)  # Extract out the author here and remove it from request.POST
        r = requests.post('http://127.0.0.1:8000/api/test/', data=post_data)
    else:
        r = requests.get('http://127.0.0.1:8000/api/test/', data=request.GET)

    if r.status_code == 201 and request.method == 'POST':
        data = r.json()
        testsave_attrs = {
            "book": data["book"],
            "author": author
        }
        # You should probably do some validation that author is not None here
        # before you actually attempt to create T1.
        # You could do it here or anywhere else before creating T1.
        testsave= T1.objects.create(**testsave_attrs)
        return HttpResponse(r.text)
    elif r.status_code == 200:  # GET response
        return HttpResponse(r.json())
    else:
        return HttpResponse('Could not save data')

Upvotes: 2

Related Questions