Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Django Admin Action to Duplicate a Record

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

Heres the use case.

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?

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

I'm thinking something like this:

In my app:

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?

like image 648
Dave Merwin Avatar asked Oct 21 '10 16:10

Dave Merwin


1 Answers

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"
like image 148
Jeff Triplett Avatar answered Oct 06 '22 00:10

Jeff Triplett