In create form when I tried to upload files bigger than settings.FILE_UPLOAD_MAX_MEMORY_SIZE, I got FileNotFoundError(No such file or directory: '/tmp/tmp9vcn09ii.upload.jpg')
After some digging the problem is in form.create_or_edit_object__post_handler the double-save for is_create, the second save is saving the file again.
If the file is smaller than settings.FILE_UPLOAD_MAX_MEMORY_SIZE, it gets saved twice (eg uyuni.jpeg and uyuni_KzLJV52.jpeg).
If the file is bigger, the first save copies the file and removes the /tmp/ file, so the second save raises an exception in django/core/files/move.py L61: with open(old_file_name, "rb") as old_file:
I suppose you need to skip FileField / ImageField in the
for field in values(form.fields):
# two phase save for creation in django, have to save main object before related stuff
if not field.extra.get('django_related_field', False):
form.apply_field(field=field, instance=form.instance)
or in form.apply(form.instance), but only for is_create
EDIT:
since it's hard to tell where to skip it and also there might be some custom model fields that will need it, how about something like:
Maybe a safe_phase or save_method enum that could have some reasonable semantic values that we interpret differently for save/create depending on what makes sense?
In create form when I tried to upload files bigger than
settings.FILE_UPLOAD_MAX_MEMORY_SIZE
, I gotFileNotFoundError(No such file or directory: '/tmp/tmp9vcn09ii.upload.jpg')
After some digging the problem is in
form.create_or_edit_object__post_handler
the double-save foris_create
, the second save is saving the file again. If the file is smaller thansettings.FILE_UPLOAD_MAX_MEMORY_SIZE
, it gets saved twice (eguyuni.jpeg
anduyuni_KzLJV52.jpeg
). If the file is bigger, the first save copies the file and removes the/tmp/
file, so the second save raises an exception indjango/core/files/move.py
L61:with open(old_file_name, "rb") as old_file:
I suppose you need to skip
FileField
/ImageField
in theor in
form.apply(form.instance)
, but only foris_create
EDIT: since it's hard to tell where to skip it and also there might be some custom model fields that will need it, how about something like:
then if you skip it in first save, you can use
obj.pk
inupload_to
, which is very useful imo 🤔