Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 611
sesh Avatar asked Oct 07 '08 23:10

sesh


People also ask

What we can do in admin portal in Django?

Overview. The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

What is __ Str__ in Django?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model.

What is admin interface in Django?

Django provides a default admin interface which can be used to perform create, read, update and delete operations on the model directly. It reads set of data that explain and gives information about data from the model, to provide an instant interface where the user can adjust contents of the application .

What is admin Modeladmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


2 Answers

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.

like image 114
Harley Holcombe Avatar answered Oct 04 '22 15:10

Harley Holcombe


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.

like image 31
kontextify Avatar answered Oct 04 '22 14:10

kontextify