创建 Django 管理操作以复制记录 [英] Create a Django Admin Action to Duplicate a Record

查看:17
本文介绍了创建 Django 管理操作以复制记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个允许我创建记录副本的 Django 管理操作.

I want to create a Django Admin Action that allows me to create a duplicate of a record.

这是用例.

管理员点击他们想要复制的应用中记录旁边的复选框.管理员从管理员操作下拉菜单中选择复制".管理员点击去.Django 管理员使用新 ID 创建重复记录.页面被刷新,并添加了带有 id 的新副本.管理员单击新的重复记录并对其进行编辑.管理员点击保存.

Admin clicks the checkbox next to a record in an app that they want to duplicate. Admin selects "Duplicate" from the admin action drop down menu. Admin clicks go. Django admin creates a duplicate record with a new id. Page is refrshed and new duplicate is added with id. Admin clicks on the new, duplicated record, and edits it. Admin clicks save.

我是疯了还是这是一个非常直接的管理员操作?

Am I crazy or is this a pretty straight forward Admin Action?

我一直在使用这些文档作为参考:http:///docs.djangoproject.com/en/dev/ref/contrib/admin/actions/

I've been using these docs for reference: http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/

我在想这样的事情:

在我的应用中:

def duplicate(modeladmin, request, queryset):
    new = obj.id
    queryset.create(new)
    return None
duplicate.short_description = "Duplicate selected record"

我知道那不对……但我的想法是否接近?

I know that's not right... but is my thinking close?

推荐答案

你的想法是对的,但你需要遍历查询集,然后复制每个对象.

You have the right idea but you need to iterate through the queryset then duplicate each object.

def duplicate_event(modeladmin, request, queryset):
    for object in queryset:
        object.id = None
        object.save()
duplicate_event.short_description = "Duplicate selected record"

这篇关于创建 Django 管理操作以复制记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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