爲什麼不創建一個有趣的保存信號功能,它將圖像文件複製到任何你想要的,然後創建一個新的對象與一個新的圖像字段?
models.py
class ObjectOne(models.Model):
logo = models.ImageField(...)
class ObjectTwo(models.Model):
logo = models.ImageField(...)
from django.db.models.signals import post_save
from signals import my_signal
post_save.connect(my_signal, dispatch_uid="001")
signals.py
from models. import ObjectOne, ObjectTwo
def my_signal(sender, instance, *args, **kwargs):
if sender is ObjectOne:
new_obj = ObjectTwo()
new_obj.logo.save("custom/path/new_filename.jpg",File(open(instance.image.url,"w"))
new_obj.save()
我沒有測試過的圖像複製代碼,但這是一般的想法。還有更多在這裏:
Programmatically saving image to Django ImageField
Django: add image in an ImageField from image url
how to manually assign imagefield in Django