1

I know how we can add an action to duplicate a record as explaind below: Create a Django Admin Action to Duplicate a Record To edit the added record one must find and edit it manually. I'd like to know how we can edit the added record as a part of this action ie. the action saves the record and calls the change view for this record(save and edit)

Community
  • 1
  • 1
user2012749
  • 77
  • 1
  • 3
  • Hi user2012749 - could you mark my answer as correct, so that this question is marked as answered? Stackoverflow relies on this mechanism, and it's considered rude not to get a correct answer and not formally recognise it. – spookylukey Dec 22 '14 at 10:25

1 Answers1

4

I'm not sure what you want to do if there are multiple added records. However, for the single case, you could just do a redirect to the admin page. You need to see the section in the docs about showing further pages:

https://docs.djangoproject.com/en/1.7/ref/contrib/admin/actions/#actions-that-provide-intermediate-pages

You'll need to do a redirect to edit the newly created object. Combined with the duplicating code, it's going to look something like:

def duplicate_records(modeladmin, request, queryset):
    object_ids = []
    for object in queryset:
        object.id = None
        object.save()
        object_ids.append(object.id)

    if len(object_ids) == 1:
        return HttpResponseRedirect(reverse('admin:yourapp_yourmodel_change', 
                                    args=(object_ids[0],))
    else:
        return HttpResponseRedirect(reverse('admin:yourapp_yourmodel_changelist')
            + "?id_in={0}".format(",".join(str(i) for i in object_ids))

This code is untested, and you'll need some imports, but it should work. For the multiple object case, it's supposed to redirect to a changelist page that shows just the created objects.

spookylukey
  • 6,380
  • 1
  • 31
  • 34