Django“另存为新”并保留图片字段 [英] Django "Save as new" and keep Image fields

查看:61
本文介绍了Django“另存为新”并保留图片字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个ImageField的Django模型。

I have a Django model with multiple ImageFields.

在ModelAdmin类上,我已设置 save_as = True ,这意味着管理页面上有一个另存为新按钮,它允许复制现有项目并将其另存为新项目。

On the ModelAdmin class I've set save_as = True, which means The admin page has a "Save as new" button, which allows for duplicating an existing item and saving it as new.

但是何时使用此按钮,ImageFields不重复,并且在新项目上留为空白。

However when this button is used, the ImageFields are not duplicated and are left blank on the new item.

看着POST请求,我发现这些字段在post数据中是空白的。

Looking at the POST request, I see that these fields are blank in the post data.

我考虑过重写Model类的save方法,并由我自己复制旧对象中的图像。但是据我所知,我没有办法告诉该对象是新保存的。我似乎也没有旧商品的ID,因此无法从中获取旧图片。

I've thought about overriding the Model class' save method, and copying the images from the old object by myself. But as far as I could figure out, I have no way to tell that the object is saved "as new". I also don't seem to have the ID of the old Item so I cannot get the old Images from it.

有没有办法将这些图片字段设为

Is there a way to get these image fields to be duplicated as well?

编辑:
根据请求添加示例代码。

Added Example code by request.

创建仅具有一个模型的简约应用程序。验证的问题仍然存在。

Created a minimalistic app with only one model. Verified problem still occurs.

样本模型。py:

from django.db import models

class Person(models.Model):
    face_image = models.ImageField(upload_to='images', 
                                   null=False, 
                                   blank=True)

示例admin.py:

Sample admin.py:

from django.contrib import admin
from testapp.models import Person

class PersonAdmin(admin.ModelAdmin):
    save_as = True

admin.site.register(Person, PersonAdmin)


推荐答案

我设法找到了一些解决方法:

I've managed to find some workaround:

我已经覆盖了原始的管理表单(请参阅此处)来获取它,还应在另存为新文件中包含旧模型的ID。 POST请求。我通过为该模型创建一个特殊的管理员并在其中添加隐藏的输入来做到这一点:

I've overridden the original admin form (see here) to get it also include the old Model's ID in "save as new" POST request. I've did it by creating a special admin for of that model, and adding inside it a hidden input:

<input type="hidden" name="my_objectid" value="{{ object_id }}">

之后,我使ModelAdmin类加载了特定的html。然后,我重写了AdminModel类的save_model方法,因此它也将复制图像。

afterwards I've made the ModelAdmin class load that specific html. Then I overriden the AdminModel class' save_model method so it would copy the images as well.

所以新的admin.py应该如下所示:

So the new admin.py should look like this:

from django.contrib import admin
from testapp.models import Person

from django.db.models.fields.files import ImageFieldFile #added to be used later

class PersonAdmin(admin.ModelAdmin):
    save_as=True
    change_form_template = 'admin/person_change_form.html';
    def save_model(self, request, obj, form, change):       

        if '_saveasnew' in request.POST: #Django always sends this when "Save as new is clicked"
            origObjId = request.POST['my_objectid']; #Get the ID that is new posted after overriding the form. 
            originalPerson = Person.objects.get(id=origObjId); #Use the Id to get the old object
            for prop, value in vars(originalPerson).iteritems(): #iterate through all it's properties
                if isinstance(getattr(originalPerson,prop), ImageFieldFile): #if the property is an Image (don't forget to import ImageFieldFile!)
                    setattr(obj,prop,getattr(originalPerson,prop)) #Copy it!

        obj.save()

admin.site.register(Person, PersonAdmin)

这篇关于Django“另存为新”并保留图片字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆