1
You can assign a File
instance to the model field. Something like this should work:
obj = form.save(commit=False)
# [...] your encryption stuff [...]
obj.some_file_field = ContentFile(secret.serialize(), uuid.uuid4().get_hex())
obj.save()
form.save_m2m()
See also the second example of the section Handling uploaded files with a model in the Django documentation.
Be aware that larger files are not handled in-memory; they are TemporaryUploadedFile
instances.
Source:stackexchange.com