In the Django admin interface, is there a way to duplicate an item?

Just wondering if there is an easy way to add the functionality to duplicate an existing listing in the admin interface?

In data entry we have run into a situation where a lot of items share generic data with another item, and to save time it would be very nice to quickly duplicate an existing listing and only alter the changed data. Using a better model structure would be one way of reducing the duplication of the data, but there may be situation where the duplicated data needs to be changed on an individual basis in the future.


Asked by: Richard903 | Posted: 01-10-2021






Answer 1

You can save as by just enabling adding this to your ModelAdmin:

save_as = True

This replaces the "Save and add another" button with a "Save as" button. "Save as" means the object will be saved as a new object (with a new ID), rather than the old object.

Answered by: Joyce873 | Posted: 02-11-2021



Answer 2

There's a better (but not built-in) solution here:

https://github.com/RealGeeks/django-modelclone

From their README:

Django Admin has a save_as feature that adds a new button to your Change page to save a new instance of that object.

I don't like the way this feature works because you will save an identical copy of the original object (if you don't get validation errors) as soon as you click that link, and if you forget to make the small changes that you wanted in the new object you will end up with a duplicate of the existing object.

On the other hand, django-modelclone offers an intermediate view, that basically pre-fills the form for you. So you can modify and then save a new instance. Or just go away without side effects.

Answered by: Owen345 | Posted: 02-11-2021



Answer 3

You can also apply this method: https://stackoverflow.com/a/4054256/7995920

In my case, with unique constraint in the 'name' field, this action works, and can be requested from any form:


def duplicate_jorn(modeladmin, request, queryset):
    post_url = request.META['HTTP_REFERER']

    for object in queryset:
        object.id = None
        object.name = object.name+'-b'
        object.save()

    return HttpResponseRedirect(post_url)

Answered by: Dainton614 | Posted: 02-11-2021



Similar questions

user interface - Change command Method for Tkinter Button in Python

I create a new Button object but did not specify the command option upon creation. Is there a way in Tkinter to change the command (onclick) function after the object has been created?


user interface - making a python GUI

This question already has answers here:


Python file interface for strings

This question already has answers here:


user interface - Are there any good 3rd party GUI products for Python?

In .Net you have companies like DevEpxress, and Infragistics that offer a range of GUI widgets. Is there any market like that for Python GUI widgets? I'm thinking specifically about widgets like the DevExpress xtraGrid Suite. Edit 01-16-09: For Example: http://www.devexpress.com/Downloads/NET/OnlineDemos.xml


user interface - Is there a Python library for easily writing zoomable UI's?

My next work is going to be heavily focused on working with data that is best understood when organized on a two-dimensional zoomable plane or canvas, instead of using lists and property forms. The library can be based on OpenGL, GTK+ or Cairo. It should allow me to: build widgets out of vector shapes and text (perhaps even SVG based?) arrange these widgets on a 2D plane catch widget...


user interface - How to modify existing panels in Maya using MEL or Python?

I've been writing tools in Maya for years using MEL and Python. I'd consider myself an expert in custom window/gui design in Maya except for one area; modifying existing panels and editors. Typically, I'm building tools that need totally custom UIs, so its customary for me to build them from scratch. However, recently I've found myself wanting to add some additional functionality to the layer editor in Maya. I'v...


user interface - What library is best for GUI in python?

This question already has answers here:


user interface - Where can I find a GUI designer for Python?


python - What SHOULDN'T Django's admin interface be used for?

I've been applying Django's automatic administration capabilities to some applications who had previously been very difficult to administer. I'm thinking of a lot of ways to apply it to other applications we use (including using it to replace some internal apps altogether). Before I go overboard though, is there anything in particular I shouldn't use it for?


Source interface with Python and urllib2

How do i set the source IP/interface with Python and urllib2?






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top