在Django的ImageField中使用已经上传的图像 [英] Use already uploaded image in Django's ImageField
本文介绍了在Django的ImageField中使用已经上传的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ImageField中是否可以设置路径或已上传的图像?
Is there an option in ImageField to set the path or an already uploaded image?
我的用例如下:我想在两个不同的内容条目中重新使用同一图像.我不想上传图片两次,也不想存储同一图片的两个副本.
My use case is as follows: I want to reutilize the same image in two different content entries. I don't want to upload the image twice or to store two copies of the same image.
推荐答案
只需将图像字段从一个模型实例分配给另一个:
Just assign the image field from one model instance to another:
original_entry = Entry.objects.get(pk=1)
another_entry = Entry.objects.get(pk=2)
another_entry.image = original_entry.image
another_entry.save()
new_entry = Entry.objects.create(image=original_entry.image)
这篇关于在Django的ImageField中使用已经上传的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文