Ouch. In a formset sounds like you could be calling save up to N*2 times.
> I think that the commit=False (or save=False in FieldFile.save() )
> should not do the save to the storage backend.
Agreed. I have been considering treating FileFields the same as
many-to-many fields when commit=False in a ModelForm. In the event
that commit=False a new method is available to perform the file saving
explicitly. This would help in the event of wanting to name the file
the same as the primary key for instance.
>
> Another option might be to pass the save argument along to the storage
> backend and give the backend designer control over the behavior --
> perhaps to reserve a filename in a cache with a short ttl. This would
> still require that the save() on FieldFile be called more than once
> though.
We need to avoid calling save on a file backend as much as possible.
That operation may be very expensive.
Go look for a ticket about this. If there isn't one I would say report
one. This seems like a reasonable thing to do in general.
--
Brian Rosner
http://oebfare.com
Yes it would be backward incompatible. I haven't thought this all the
way through, yet. In general the solution I proposed is the ultimate
fix. For a fix like this I still need to get grips on what exactly the
process for this is going to be and when exactly it can be
implemented. A less than ideal way of accomplishing this now in user
code if they need to do this now is to override save on the ModelForm
and use their own save_instance function, but seems to be the best way
avoiding some other really ugly hack and make going forward much
simpler.
That sounds like a promising idea to me, but I'd want to see the
details. I was writing some code the other day that was trying to poke
the username into part of the upload_to path. I made it work with a
custom ModelForm __init__ method, but commit=False seems like it would
have been a lot cleaner, or at least that was my first instinct about
what to try.
To address Karen's backwards-incompatibility concern, perhaps a new
'commit_files' argument would be in order.
Joseph
Looking at the code, the issue I see with this is at the moment there
is a mechanism for the storage backend to alter the filename when
saving. If the save was performed separately after the DB update then
a second DB update would be required to store the amended name.
The default FileSystemStorage uses the name change mechanism to avoid
race conditions over conflicting names. It may be that this can be
considered unusual enough that a second DB update in this situation is
acceptable, although a mechanism for this would need to be included.
Anyway I just wanted to flag this potential issue to help with
thinking this through.
Sean