Django在视图中调用API后保存和输入数据

Django在视图中调用API后保存和输入数据,django,django-rest-framework,Django,Django Rest Framework,我有两个django项目,D1和D2。D1有一个表调用T1,D2有一个表调用T2 因此,我在D1中有一个视图,它对D2中的api进行api调用,并将POST请求后得到的值保存到T1中。但我也希望能够在表T1的其他字段中输入数据 示例:t2只有book字段,t1有book和author字段。当D1对D2中的t2执行post方法时,它将返回一个book值,该值将保存到t1中,但我还希望用户自己输入作者。我该怎么做 这是我的代码 型号.py @csrf_exempt def my_django_vie

我有两个django项目,D1和D2。D1有一个表调用T1,D2有一个表调用T2

因此,我在D1中有一个视图,它对D2中的api进行api调用,并将POST请求后得到的值保存到T1中。但我也希望能够在表T1的其他字段中输入数据

示例:t2只有
book
字段,t1有
book
author
字段。当D1对D2中的t2执行post方法时,它将返回一个
book
值,该值将保存到
t1
中,但我还希望用户自己输入
作者。我该怎么做

这是我的代码

型号.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')
在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)
在D2中

视图.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')

我相信你可以在向
my_django\u view
发出的同样的帖子请求中完成这项工作。您可以将
request.POST
视为字典。它基本上有请求中所有用户输入的键值对,可能还有CSRF令牌之类的东西。在任何情况下,在前端,您都希望有一个表单或其他东西与其他现有的
请求一起发送。发布
数据,以便在视图中提取出来

@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')

我相信你可以在向
my_django\u view
发出的同样的帖子请求中完成这项工作。您可以将
request.POST
视为字典。它基本上有请求中所有用户输入的键值对,可能还有CSRF令牌之类的东西。在任何情况下,在前端,您都希望有一个表单或其他东西与其他现有的
请求一起发送。发布
数据,以便在视图中提取出来

@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')

我是在尝试后发现这个错误的
raise AttributeError(“此QueryDict实例不可更改”)AttributeError:此QueryDict实例不可更改
此错误发生在这一行
author=request.POST.pop(“author”,None)
啊,是的,只需复制一份-我已经更新了我的答案,还请注意您的requests.POST也需要使用复制的数据。它可以工作。但是保存后查看T1表api的结果,作者的结果似乎很奇怪<代码>“author”:“['new']”“
而不是
“author”:“new”
您的数据在保存之前是什么样子的-
author
author=post_data.pop一行中返回什么内容(“author”,None)
?查看T1时,您看到的完整结果是什么?我更新了答案,注意行
author=post_data.dict().pop(“author”,None)
-我们需要先将其转换为python dict,以便从列表中提取if。Django的目的是检索同一个键的多个值,因此所有内容都将存在于一个列表中。因为我们是自己提取出来的,所以我们需要记住将其转换为python字典,Django将帮助您从列表中提取单个值
raise AttributeError(“此QueryDict实例不可更改”)AttributeError:此QueryDict实例不可更改
此错误发生在这一行
author=request.POST.pop(“author”,None)
啊,是的,只需复制一份-我已经更新了我的答案,还请注意您的requests.POST也需要使用复制的数据。它可以工作。但是保存后查看T1表api的结果,作者的结果似乎很奇怪<代码>“author”:“['new']”“
而不是
“author”:“new”
您的数据在保存之前是什么样子的-
author
author=post_data.pop一行中返回什么内容(“author”,None)
?查看T1时,您看到的完整结果是什么?我更新了答案,注意行
author=post_data.dict().pop(“author”,None)
-我们需要先将其转换为python dict,以便从列表中提取if。Django的目的是检索同一个键的多个值,因此所有内容都将存在于一个列表中。因为我们是自己提取出来的,所以我们需要记住将其转换为python字典,Django将帮助您从列表中提取单个值。真的,真的,不要从应用程序向自身发出POST请求。那么您有什么建议?因为它使用的是来自另一个项目的API真的,真的,不要从你的应用程序向自己发出POST请求。那么你有什么建议?因为它使用的是另一个项目的API