Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

330
Vistas
How to upload and save a pickle file to a django rest framework?

As it says in the title, I am trying to upload a model.pkl and save it in an API made in django. I manage to save the model.pkl correctly in the API, however the file is uploaded in a corrupt way, because I cannot read the pickle file.

Im open to any solutions that can make the pickle file be uploaded, stored and readed in the API.

Class to upload the file and save it

class FileUploadView(APIView):
    parser_classes = (FileUploadParser,)

    def put(self, request):
        file = request.data.get('file', None)
        file_name = f'path/{file.name}'

        path = default_storage.save(name=file_name, content=ContentFile(file.read()))
        tmp_file = os.path.join(settings.MEDIA_ROOT, path)

        if file is not None:
            return Response(f'File: {file.name} successfully uploaded and saved!', status=HTTP_200_OK)
        else:
            return Response(f'File not found!', status=HTTP_400_BAD_REQUEST)

Error when reading the file

def read_PickleModel(path_to_PickleModel):
    with open(path_to_PickleModel, 'rb') as f:
        pickle_model = pickle.load(f)
    return pickle_model

read_PickleModel(path_to_PickleModel)

Traceback:
DEBUG: An exception has ocurred: invalid load key, '-'.

Postman

enter image description here enter image description here

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

When using FileUploadParser the entire body of the request should be the file contents, don't send the file as form data.

To do this in Postman, select "binary" as the data type instead of "form-data" and select your file there

enter image description here

To set the filename you need to set a header "Content-Disposition" and pass attachment; filename=<your_filename.ext>

enter image description here

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda