I'm trying to save the canvas object to my django model field file but it says "'Canvas' object has no attribute '_committed'".
p.setTitle(f"{patient.first_name} {patient.last_name}'s Report")
p.showPage()
p.save()
pdf:bytes =buffer.getvalue()
buffer.close()
response.write(pdf)
r = Result.objects.filter(score="12").update_or_create(file=p)
Can anyone help me out with this issue?
CodePudding user response:
"You must pass an instance of django's File object to FileField.save()
to change the content of a file field. It works a bit differently from other types of model fields." Quoted from here
CodePudding user response:
You can not use an .update_or_create(…)
[Django-doc] after a .filter(…)
[Django-doc]. What you probably wanted is to use:
Result.objects.update_or_create(
score='12',
defaults={'file': p}
)
This will however not work with p
: p
is not a file, it is a Canvas
object. You can use the file name, or work with a FieldFile
object [Django-doc].
CodePudding user response:
Well instead of using
buffer =BytesIO()
buffer.close()
I used
buffer.seek(0)
r = Result.objects.get(pk=result_id)
r.file.save(f"{d}.pdf", File(buffer))
and it worked.