vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.25k stars 431 forks source link

Uploading imag with extra fields #498

Open samandar021 opened 2 years ago

samandar021 commented 2 years ago

I am gonna add avatar image to django object , but I cannout PUT image file with extra fields, it is showing error, how can I fix It? @vitalik

@user_router.put('/profile',
                 response={200: Response[str], 409: Response[Error]},
                 auth=JwtAuthBearer())
def update_profile(request, payload: UserIn, image: Optional[UploadedFile] = File(None)):
    user_id = request.auth
    user = Identity.objects.filter(id=user_id).update(
        first_name=payload.first_name,
        last_name=payload.last_name,
        company_name=payload.company_name,
        # TODO  in current version project edit email is disabled
        # email=payload.email,
        phone_number=payload.phone_number,
    )
    return empty_response()`

and it is giving error like this

{
  "detail": [
    {
      "loc": [
        "body",
        "payload"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    }
  ]
}

422Undocumented | Error: Unprocessable Entity in Swagger UI

vitalik commented 2 years ago

@samandar021

Django by default do not parse files if method is not POST

you can fix this with custom middleware:

https://github.com/vitalik/django-ninja/issues/417#issuecomment-1092545699

sc231997 commented 3 months ago

Got across the same scenario maybe giving a parameter in ninja to enable file handling in PUT call as well will be a great enhancement.