简体   繁体   English

Django REST Swagger如何处理响应POST api(基于函数)

[英]Django REST Swagger how to process response POST api (function based)

I am trying to accept a image file that has been post using my Django REST function based POST API. 我正在尝试接受使用基于Django REST函数的POST API发布的图像文件。 This is based on https://github.com/m-haziq/django-rest-swagger-docs 这是基于https://github.com/m-haziq/django-rest-swagger-docs

I am getting this error screenshot ( https://imgur.com/a/ECq5y ) 我正在获取此错误屏幕截图( https://imgur.com/a/ECq5y

Object of type 'TypeError' is not JSON serializable

for doing this 为此

face_image = request.data.get('face_image')

and whats the right step to save it to the model, would it be something like this 以及将其保存到模型的正确步骤是什么?

employee.face_image = face_image

Here is how I define the API 这是我定义API的方式

@api_view(['POST'])
def update_employee_image(request):
    # ----- YAML below for Swagger -----
    """
    description: This API deletes/uninstalls a device.
    parameters:
      - name: employee_id
        type: integer
        required: true
        location: form
      - name: face_image
        type: file
        required: true
        location: form
    """
    employee_id = request.POST.get('employee_id')
    face_image = request.data.get('face_image') <--- this causes the error

Here is the model with the imagefield 这是带有像场的模型

class Employee(models.Model):
    ....
    face_image = models.ImageField(upload_to='face_image/', blank=True)

Can someone let me know the right way to do this ? 有人可以让我知道正确的方法吗? process the image from the post and save it to the model. 处理帖子中的图像并将其保存到模型中。 My full source code is in the those links. 我的完整源代码在这些链接中。 Thanks. 谢谢。

FileUploadParser solves this issue and able to accept the image post FileUploadParser解决了此问题,并能够接受图像发布

parser_classes = (FileUploadParser,)
face_image_obj = request.data['face_image']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM