Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

328
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!