Django FileField在上传后移动文件 [英] Django FileField move file following upload
问题描述
围绕该主题有几个问题,但我发现没有一个适合我的尝试。我希望能够将文件上传到模型,并使用模型实例属性(例如pk)将该文件保存在一个不错的位置。我知道这些东西是在 model.save()
之后设置的,因此我需要编写一个自定义保存来执行此操作,但我无法弄清楚。这是我的东西:
There are several questions around this topic but not one that I've found that suits what I'm trying to do. I want to be able to upload a file to a model and save that file in a nice location using the model instance attributes like pk. I know that this stuff gets set after model.save()
so I need to write a custom save to do this, but I can't figure it out. Here is what I have:
class UploadModel(models.Model):
image = models.ImageField(upload_to='uploads')
def save(self, *args, **kwargs):
# Call standard save
super(UploadModel, self).save(*args, **kwargs)
if 'uploads' in self.image.path:
initial_path = self.image.path
# New path in the form eg '/images/uploadmodel/1/image.jpg'
new_path = os.path.join(settings.MEDIA_ROOT, 'images',
self._meta.model_name, self.pk, os.path.basename(initial_path))
# Create dir if necessary and move file
if not os.path.exists(os.path.dirname(new_path)):
makedirs(os.path.dirname(new_path))
os.rename(initial_path, new_path)
# Do something here to save the new file to the image field
# Save changes
super(UploadModel, self).save(*args, **kwargs)
我有什么对 image
字段进行操作以使其引用此新文件位置,并为其设置所有有用的属性,例如 image.path
, image.name
, image.url
等?
What do I have to do to the image
field to get it to reference this new file location, and set all the useful attributes to it like image.path
, image.name
, image.url
etc?
文档说上述就是我所要做的,但这只会导致 image
字段指向一个不存在的文件。我查看了与此相关的问题,并尝试了片段,但我还没有找到解决方案。
The docs say that the above is all I should need to do but this just results in the image
field pointing to a file that doesn't exist. I've looked at this related question and tried the snippet mentioned in one of the answers but I have not found a solution yet.
推荐答案
我经过大量搜索和发现此旧文档票证有很好的解释。
I figured it out after a lot of searching and finding this old documentation ticket that has a good explanation.
class UploadModel(models.Model):
image = models.ImageField(upload_to='uploads')
def save(self, *args, **kwargs):
# Call standard save
super(UploadModel, self).save(*args, **kwargs)
if 'uploads' in self.image.path:
initial_path = self.image.path
# New path in the form eg '/images/uploadmodel/1/image.jpg'
new_name = '/'.join(['images', self._meta.model_name, str(self.id),
path.basename(initial_path)])
new_path = os.path.join(settings.MEDIA_ROOT, 'images',
self._meta.model_name, self.pk, os.path.basename(initial_path))
# Create dir if necessary and move file
if not os.path.exists(os.path.dirname(new_path)):
makedirs(os.path.dirname(new_path))
os.rename(initial_path, new_path)
# Update the image_file field
self.image_file.name = new_name
# Save changes
super(UploadModel, self).save(*args, **kwargs)
现在我读了<一个href = https://docs.djangoproject.com/zh-CN/1.8/topics/files/#using-files-in-models rel = nofollow>为此的文档,它看起来很明显:)但我确实认为可以使说明更具描述性。希望这可以节省一些时间!
Now I read the docs for this it looks totally obvious :) but I do think the explanation could be made more descriptive. Hopefully this will save someone some time!
这篇关于Django FileField在上传后移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!