Python Django Form Wizard: How can I change the filename of an filefield before step data is saved?

StackOverflow https://stackoverflow.com/questions/21328540

Question

I have a problem concerning the formwizard. I am using a formwizard with 6 steps and in the 5th step there is a file field. Everything works fine except when I try to upload files with filenames that contain non-ascii characters. I wrote a method that replaces the non-ascii characters and put it into the save-method of my model. That also works.

The problem now is, that the form wizard uploads the file to a temp-dir before the data from all forms is finally saved. Here the save-method of the model is not called and therefore the filename is not changed and I get an error. How can I change the filename before the data of the step saved?

Update: I had to configure my Apache so that it accepts non-ASCII file names - https://docs.djangoproject.com/en/1.4/howto/deployment/modpython/#if-you-get-a-unicodeencodeerror

Was it helpful?

Solution

Here is sample code . You can replace logo.jpg with file name too :

user.id_file.path.save("user/"+str(user.id)+"/logo.jpg", form.cleaned_data['logo'])
user.id_file.save()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top