8

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

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

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

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

我考虑过覆盖 Model 类的 save 方法,并自己从旧对象中复制图像。但据我所知,我无法判断该对象是“作为新的”保存的。我似乎也没有旧项目的 ID,所以我无法从中获取旧图像。

有没有办法让这些图像字段也被复制?

编辑: 按要求添加示例代码。

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

示例模型.py:

from django.db import models

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

示例 admin.py:

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

class PersonAdmin(admin.ModelAdmin):
    save_as = True

admin.site.register(Person, PersonAdmin)
4

5 回答 5

9

响应的基础上,这是实现相同结果的更通用方法:

from django.core.urlresolvers import resolve
from django.db.models.fields.files import FieldFile

class PersonAdmin(admin.ModelAdmin):
    save_as = True

    def save_model(self, request, obj, form, change):
        # Django always sends this when "Save as new is clicked"
        if '_saveasnew' in request.POST:
            # Get the ID from the admin URL
            original_pk = resolve(request.path).args[0]
            # Get the original object
            original_obj = obj._meta.concrete_model.objects.get(id=original_pk)

            # Iterate through all it's properties
            for prop, value in vars(original_obj).iteritems():
                # if the property is an Image (don't forget to import ImageFieldFile!)
                if isinstance(getattr(original_obj, prop), FieldFile):
                    setattr(obj,prop,getattr(original_obj, prop)) # Copy it!
        obj.save()

这应该适用于任何模型和任何文件类型。它也不需要编辑表单或模板。这是合并拉取请求后不需要的解决方法:https ://github.com/django/django/pull/2246 。

于 2014-06-11T11:41:16.060 回答
7

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

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

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

之后我让 ModelAdmin 类加载特定的 html。然后我重写了 AdminModel 类的 save_model 方法,这样它也会复制图像。

所以新的 admin.py 应该是这样的:

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)
于 2013-09-03T15:44:56.000 回答
4

如果你在 2019 年在这里 .. 更新了 @nicolaslara 的答案 这个答案是 Django 2+ 和 python 3

要从 Django 管理员获取 Url,我们应该使用:

original_pk = request.resolver_match.kwargs['object_id']

并且 iteritems() 不能在 python3 上工作,我们只能使用 items()

最终代码:

  def save_model(self, request, obj, form, change):
    # Django always sends this when "Save as new is clicked"
    if '_saveasnew' in request.POST:
        # Get the ID from the admin URL
        original_pk = request.resolver_match.kwargs['object_id']
        print(original_pk)

        # Get the original object
        original_obj = obj._meta.concrete_model.objects.get(id=original_pk)

        # Iterate through all it's properties
        for prop, value in vars(original_obj).items():
            # if the property is an Image (don't forget to import ImageFieldFile!)
            if isinstance(getattr(original_obj, prop), ImageFieldFile):
                setattr(obj, prop, getattr(original_obj, prop))  # Copy it!
    obj.save()
于 2019-10-02T07:52:14.113 回答
3

这是一张描述同样问题的票: «Admin inlines with file/image field failed to save_as»

2014 年 2 月 9 日有一个拉取请求修复了这个错误。希望尽快合并。

于 2014-02-09T20:57:36.140 回答
0

这对我帮助很大,我使用了@ShravaN 的解决方案并将其扩展为也将图像保存在相关的内联模型中。我认为代码不是最好的,但它可以工作。如果您有任何改进的想法,请做!

def save_model(self, request, obj, form, change):
    # Django always sends this when "Save as new is clicked"
    if '_saveasnew' in request.POST:
        # Get the ID from the admin URL
        original_pk = request.resolver_match.kwargs['object_id']
        # Get the original object
        original_obj = obj._meta.concrete_model.objects.get(id=original_pk)
        # Iterate through all it's properties
        self._copy_image_fields(obj, original_obj)

    obj.save()

def _copy_image_fields(self, obj, original_obj):
    for prop, value in vars(original_obj).items():
        # if the property is an Image
        # (don't forget to import ImageFieldFile!)
        if isinstance(getattr(original_obj, prop), ImageFieldFile):
            setattr(obj, prop, getattr(original_obj, prop))  # Copy it!

def save_related(self, request, form, formsets, change):
    if '_saveasnew' in request.POST:
        # Get the ID from the admin URL
        original_pk = request.resolver_match.kwargs['object_id']

        # Get the original object
        original_obj = form.instance._meta.concrete_model.objects.get(
            id=original_pk
        )
        form.save_m2m()
        for formset in formsets:
            instances = formset.save(commit=False)

            if instances:
                related = list(filter(lambda r: r.related_model == formset.model, original_obj._meta.related_objects))
                related = related[0] if related else None
                # related: ManyToOneRel
                if related:
                    field_name = f"{related.name}_set" if not related.related_name else related.related_name
                    related_set = getattr(original_obj, field_name)
                else:
                    # TODO: warning?
                    continue
                for ori, ni in zip(related_set.all(), instances):
                    # instance: Model
                    # we need to figure out which field is in the original
                    # object
                    self._copy_image_fields(ni, ori)
                    ni.save()
            formset.save_m2m()
    else:
        super(LiveEventAdmin, self).save_related(request, form, formsets, change)
    
于 2020-07-29T15:22:12.783 回答