Django 1.11
When FileUpdate is called, I substitute the old file with a new one. I'd like to handle the old file. Namely move it to some other directory.
Could you have a look at the picture below. New file is called "C book 1.pdf". Existing file is called "4_Oracle_Database_11g_PLSQL_Fundamentals_PLSQL.pdf".
But at the breakpoint (see comment in form_valid), old_file_name = C book 1.pdf.
But I can't catch why. I call super(FileUpdate, self).form_valid(form) in a couple of lines. And only there self.object = form.save().
At the breakpoint I expect self.object to be the unchanged object. Existing one.
Could you help me understand how to catch the file name of the old file.
class UserFile(models.Model):
uuid = models.UUIDField(primary_key=False, default=uuid.uuid4,
editable=False)
user_file = models.FileField(verbose_name=_("file"),
max_length=255,
upload_to=get_file_path)
class FileUpdate(UpdateView):
model = UserFile
form_class = FileForm
def form_valid(self, form):
"""
Delete old file.
"""
parent_directory = get_parent_directory_of_file(self.object)
old_file_name = self.object.user_file.name # Breakpoint
self.object.user_file.delete()
return super(FileUpdate, self).form_valid(form)On Sunday 07 May 2017 16:10:27 Nonverbis M wrote:
> class FileUpdate(UpdateView):
> model = UserFile
> form_class = FileForm
>
> def form_valid(self, form):
> """
> Delete old file.
> """
> parent_directory = get_parent_directory_of_file(self.object)
> old_file_name = self.object.user_file.name # Breakpoint
> self.object.user_file.delete()
> return super(FileUpdate, self).form_valid(form)
I've battled with this too and by far the simplest way to solve it, is to put the old file name in the form as a hidden widget.
--
Melvyn Sopacua