Skip to content
Advertisement

Converting bytes to file in Django / Python

In my Django app, I have a PDF in bytes:

print(myPDF)
> b'%PDF-1.3n1 0 objn<<n/Type /Pagesn/Count 2....

I want to save it in my database:

obj.document = myPDF
obj.save()

However I keep getting an 'bytes' object has no attribute '_committed' error on save.

Advertisement

Answer

The answer was a built-in Django function, ContentFile.

from django.core.files.base import ContentFile
...

obj.document = ContentFile(myPDF, '........pdf')
obj.save()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement