I have a question that I don’t find answer on the web.
Is it possible to change a value of a Formset after the user validation ?
in views.py :
for f in my_formset:
I perform to display the cleaned data but I don’t know how to change a filed before saving …
Why I want that ?
One field contain a file and I want to store it in different directory depending on the file (I check the file type)
Thanks for your help
A formset is a collection of forms. Yes, you have access to each of those forms, and each of those forms work like any other Django form.
If this is a model formset, then each field in the form can be altered by changing the
cleaned_data field before the form is saved. Saving a formset returns the set of instances which can be subsequently changed before being saved again. (See Saving objects in the formset.)
Thanks for your help Ken
It s work find
But how can I modify a FieldFile ??
In Models.py :
file = models.FileField(upload_to='')
I try a lot af thing but without success
instance.file = '/Volumes/DIR_Main_txt/' + instance.file
instance.file = '/Volumes/DIR_Main_txt/' + str(instance.file)
A FileField is an object, not just a string with a path.
First, review the FileField docs to see what attributes are available in that field.
See all of:
Then, think very carefully about what it is that you’re trying to do to that field and what you’re thinking of changing and how you’re going about changing it.
(Hint: Once the uploaded file data is saved, it’s already in the original “upload_to” directory. If you’re changing its directory after being saved, you need to move the file and not just change the directory name entry in the field.)