代码之家  ›  专栏  ›  技术社区  ›  Axil

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

  •  0
  • Axil  · 技术社区  · 7 年前

    我正在尝试接受使用基于Django REST函数的post API发布的图像文件。这是基于 https://github.com/m-haziq/django-rest-swagger-docs

    我得到了这个错误截图( https://imgur.com/a/ECq5y )

    Object of type 'TypeError' is not JSON serializable
    

    感谢你这么做

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

    那么,将其保存到模型中的正确步骤是什么呢

    employee.face_image = face_image
    

    下面是我如何定义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
    

    这是带有imagefield的模型

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Axil    7 年前

    FileUploadParser解决了这个问题,并且能够接受图像发布

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