python 在 Django 管理界面中,有没有办法复制一个项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/180809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:37:11  来源:igfitidea点击:

In the Django admin interface, is there a way to duplicate an item?

pythondjangodjango-modelsdjango-admin

提问by sesh

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.

在数据输入中,我们遇到过很多项目与另一个项目共享通用数据的情况,为了节省时间,快速复制现有列表并仅更改更改的数据会非常好。使用更好的模型结构将是减少数据重复的一种方法,但将来可能会出现需要单独更改重复数据的情况。

回答by Harley Holcombe

You can save asby just enabling adding this to your ModelAdmin:

您可以通过启用将其添加到您的 ModelAdmin来另存为

save_as = True

This replaces the "Save and add another" button with a "Save as" button. "Save as" means the object will be saved as a new object (with a new ID), rather than the old object.

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

回答by kontextify

There's a better (but not built-in) solution here:

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

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

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

From their README:

从他们的自述文件:

Django Admin has a save_asfeature that adds a new button to your Change page to save a new instance of that object.

I don't like the way this feature works because you will save an identical copy of the original object (if you don't get validation errors) as soon as you click that link, and if you forget to make the small changes that you wanted in the new object you will end up with a duplicate of the existing object.

On the other hand, django-modelclone offers an intermediate view, that basically pre-fills the form for you. So you can modify and then save a new instance. Or just go away without side effects.

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

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

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

回答by Abel

You can also apply this method: https://stackoverflow.com/a/4054256/7995920

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

In my case, with unique constraint in the 'name' field, this action works, and can be requested from any form:

在我的情况下,在“名称”字段中使用唯一约束,此操作有效,并且可以从任何形式请求:



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)