I can't upload an image to my database at all

I'm using django ninja back end the code works but when I try to send it through an application that I'm making using javascript or even with bash it doesn't work

i've tryed

`def upload_media(request, nome:str, imgurl: UploadedFile ):

usuarios = Media.objects.all()
Media.objects.create(nome = nome,imgurl=imgurl)

return 1`

and

class mediaSchema(Schema): nome : str imgurl: UploadedFile

`@api.post('tst/', response=mediaSchema) def upload_media2(request, infos : mediaSchema):

media = infos.dict() infos = Media(**media)

infos.save()

return 1`

Issues with code:

  • The UploadedFile type in imgurl isn't handled correctly.UploadedFile must be managed through Django's request.FILES, not directly as a parameter. The function retrieves usuarios = Media.objects.all() but doesn’t use it. This line is redundant. Also there’s no error handling for file uploads or invalid data. lasn but not least, the return value 1 is not meaningful.

    from django.http import JsonResponse
    from django.core.files.uploadedfile import UploadedFile
    
    def upload_media(request, nome: str, imgurl: UploadedFile):
        if request.method != "POST":
            return JsonResponse({"error": "Only POST method allowed"}, status=405)
    
      # Ensure file is uploaded via request.FILES
      if not request.FILES.get("imgurl"):
          return JsonResponse({"error": "File not uploaded"}, status=400)
    
      # Save the uploaded file
      imgurl = request.FILES["imgurl"]  # Retrieve the file from request
      media = Media.objects.create(nome=nome, imgurl=imgurl)
    
      return JsonResponse({"message": "Media uploaded successfully", "media_id": media.id})
    
  • class mediaSchema and upload_media2 issues: UploadedFile in mediaSchema doesn't work as expected with Django Ninja. Ninja uses File(...) to handle file uploads. infos.dict() won't handle UploadedFile properly because it isn’t a serializable field. Attempting to directly create a Media object from mediaSchema (Media(**media)) is incorrect because imgurl is not a compatible field

    from ninja import Schema, File
    from django.core.files.uploadedfile import UploadedFile
    from ninja import ModelSchema
    
    class MediaSchema(Schema):
        nome: str
        imgurl: UploadedFile
    
    @api.post('tst/', response={200: str, 400: str})
    def upload_media2(request, nome: str, imgurl: UploadedFile = File(...)):
        try:
          media = Media.objects.create(nome=nome, imgurl=imgurl)
          return 200, "Media uploaded successfully!"
      except Exception as e:
          return 400, f"Error occurred: {str(e)}"
    
Вернуться на верх