2👍
✅
Django FileField
would either be a django.core.files.File
, which is a file instance or django.core.files.base.ContentFile
, which takes a string as parameter and compose a ContentFile
. Since you already had the file content as a string, sounds like ContentFile
is the way to go(I couldn’t test it but it should work):
from django.core.files.base import ContentFile
# create an in memory instance
css = form.save(commit=False)
# file content as string
css_string = "body {color: #a9f;}"
# create ContentFile instance
css_file = ContentFile(css_string)
# assign the file to the FileField
css.css_file.save('myfile.css', css_file)
css.save()
Check django doc about FileField details.
Source:stackexchange.com