zmathew / django-ajax-upload-widget

Provides AJAX file upload functionality for FileFields and ImageFields with a simple widget replacement in the form.
BSD 3-Clause "New" or "Revised" License
89 stars 63 forks source link

fix: update a model with a already uploaded file field untouched. #2

Closed laishulu closed 11 years ago

laishulu commented 11 years ago

for example, a FileField has parameter such as upload_to='img'.

If you newly upload a new file it will all be ok, because in the ajax post, the url of "ajax_uploads/some_uuid_filename.jpg" will be used.

But if you have already uploaded the file, and just want to updata other fields in the form, the form will be initialized with "img/some_uuid_filename.jpg" for the file field. and the code:

uploaded_file = UploadedFile.objects.get(file=relative_path)

will failed, because all files in UploadedFile are in fact "ajax_uploads/some_uuid_filename.jpg"

A field not in the form of "ajax_uploads/some_uuid_filename.jpg" has no newly uploaded file, thus we should ignored it.

zmathew commented 11 years ago

I've fixed this in caef5b in a slightly different way. If you submit the already uploaded file it will leave the field unchanged.

Try it out and let me know if it resolves your issue. If all is good I'll make a new pypi release.

laishulu commented 11 years ago

yah, it works.

But it will add a light chance to fail silently when the file been removed between being uploaded to ajax_uploads and laterly ajax post.

files in ajax_uploads are not used later, so it's likely to be removed by some cron jobs.

zmathew commented 11 years ago

Right, but I think it is acceptable because the server should not delete the temporarily uploaded file until sufficient time has passed that it can assume the file is not needed.

I'm planning on writing a management command to do this clean up that should help resolve that.