English 中文(简体)
将图像现场的内容复制到Django的新档案途径
原标题:Copy the contents of an ImageField to a new file path in Django

我有一个Django模型标的,具有几个正常属性,它有一个标识图像场。 我想写出一种将这个物体复制到新物体的方法。 它很容易使新物体化,然后从旧到新(例如new_object.name = 旧的物体:)。 然而,如果在标识领域(即:new_object.logo = 旧_object.logo)下,新元和旧的元件都与硬盘上相同的文档相提。 如果你编辑旧的档案,新物体的标识将发生变化。

什么是全程/完整版图像现场的最佳途径? 我希望它有不同的档案名称,在磁盘上注明不同的档案,但其内容应当完全相同。

最佳回答

为什么不产生一种功能,而这种功能取决于什么是你想要什么地方来复制file<>>/strong>的信号,那么就会产生一个具有新形象的新物体?

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()

我没有测试图像复制法,但这是一般的想法。 这里还有:

用于Django图像现场的方案节约图像

Django: 在图像领域添加图像,从图像url

how to Manuallysignfield in Django

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...