簡體   English   中英

Django REST Swagger如何處理響應POST api(基於函數)

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

我正在嘗試接受使用基於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)

有人可以讓我知道正確的方法嗎? 處理帖子中的圖像並將其保存到模型中。 我的完整源代碼在這些鏈接中。 謝謝。

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