#Adding k,v from form.cleaned_data.items() sometimes gives non-JSON Serializable errors on files

2 messages · Page 1 of 1 (latest)

dense venture
#

I'm running into a strange issue that a TemporaryUploadedFile is not JSON Serializable. I'm confident that this is caused by a file slipping into the context (and not being able to serialize it), but I can't figure out how it's happening. This is more or less my code:

for k,v in form.cleaned_data.items()
   is_file = (check the input_type of the key on the form field to see if this should be a file) 
  if is_file:
   if value is not None:
     for file in value:
       (Upload the file to S3 storage)
       if only one file:
         request.session[k] = file.name
       else:
         request.session[k] = f"{count} files"
   else:
    request.session[k] =
  else:
   request.session[k] = v 

Based on this, I feel like either file.name is somehow sometimes coming back with the file itself, or is_file is sometimes coming back as false. Has anyone else seen this?

#

The specific error for reference is

TypeError: Object of type TemporaryUploadedFile is not JSON serializable

But I can't fathom for the life of me how that code would allow a file to slip through. It's not a fundamental problem because most users are able to press through without issue.