76

Just wondering if there is an easy way to add the functionality to duplicate an existing listing in the admin interface?

In data entry we have run into a situation where a lot of items share generic data with another item, and to save time it would be very nice to quickly duplicate an existing listing and only alter the changed data. Using a better model structure would be one way of reducing the duplication of the data, but there may be situation where the duplicated data needs to be changed on an individual basis in the future.

4

3 回答 3

135

You can save as by just enabling adding this to your ModelAdmin:

save_as = True

这将“保存并添加另一个”按钮替换为“另存为”按钮。“另存为”表示对象将被保存为新对象(具有新 ID),而不是旧对象。

于 2008-10-07T23:26:15.867 回答
9

这里有一个更好的(但不是内置的)解决方案:

https://github.com/RealGeeks/django-modelclone

从他们的自述文件中:

Django Admin 有一个save_as功能,可以在更改页面中添加一个新按钮来保存该对象的新实例。

我不喜欢此功能的工作方式,因为您将在单击该链接后立即保存原始对象的相同副本(如果您没有收到验证错误),并且如果您忘记进行您所做的小更改想要在新对象中,您最终会得到现有对象的副本。

另一方面,django-modelclone 提供了一个中间视图,它基本上为您预先填写表单。所以你可以修改然后保存一个新实例。或者只是离开而没有副作用。

于 2018-04-10T11:10:08.127 回答
0

您也可以应用此方法:https ://stackoverflow.com/a/4054256/7995920

就我而言,在“名称”字段中具有唯一约束,此操作有效,并且可以从任何形式请求:


def duplicate_jorn(modeladmin, request, queryset):
    post_url = request.META['HTTP_REFERER']

    for object in queryset:
        object.id = None
        object.name = object.name+'-b'
        object.save()

    return HttpResponseRedirect(post_url)

于 2019-06-12T16:04:02.750 回答