views:

25

answers:

1

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?

A: 

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"
Jeff Triplett
Awesome! Thanks so much.
Dave Merwin
Now the trick is, how to also duplicate any FK models that point to this model.
Dave Merwin