【发布时间】:2021-01-23 03:43:55
【问题描述】:
我需要在views.py 中编写一个文件,并在createView 中将该文件设置为filefield。这是我的代码的样子:
models.py:
class A(models.Model):
data = models.CharField()
file = models.FileField(upload_to='%Y/%m/%d/', blank=True, null=True)
views.py:
class ACreateView(CreateView):
def form_valid(self, form):
new_file_path = compose_a_new_file() ## make new file saved somewhere.
form.instance.file.path = new_file_path
result = super().form_valid(form)
return result
这里new_file_path是新合成并保存在文件夹中的路径。
我只想用新合成的文件填充文件文件字段,
并将其保存到upload_to='%Y/%m/%d/'定义的路径中。 (就像我从客户端通过 POST 方法上传一样)
但上面的代码并没有将文件保存到'%Y/%m/%d/'文件夹中。
我怎样才能得到它? 任何建议将不胜感激。
【问题讨论】:
标签: python django file upload filefield