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)
Asked
Active
Viewed 1,688 times
1
-
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 Answers
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:
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
-
Thank you. It works. I need only the single object case but I will try for the multiple case also. – user2012749 Dec 17 '14 at 21:31