【问题标题】:Django REST Swagger how to process response POST api (function based)Django REST Swagger 如何处理响应 POST api(基于函数)
【发布时间】:2017-11-18 09:37:38
【问题描述】:

我正在尝试接受使用基于 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

这是带有图像域的模型

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

有人可以告诉我正确的方法吗?处理帖子中的图像并将其保存到模型中。我的完整源代码在这些链接中。谢谢。

【问题讨论】:

    标签: python django rest swagger imagefield


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 2020-02-16
      • 2015-10-06
      • 2019-06-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2019-09-24
      相关资源
      最近更新 更多