Duplicating model instances and their related objects in Django / Algorithm for recusrively duplicating an object

后端 未结 17 1003
名媛妹妹
名媛妹妹 2020-11-29 01:04

I\'ve models for Books, Chapters and Pages. They are all written by a User:

from django.db import models
         


        
17条回答
  •  一向
    一向 (楼主)
    2020-11-29 01:31

    There is an option to create a duplicate/clone/save-as-new in django admin.

    1. Create a ModelAdmin class of the model you want to clone in admin.py
    2. In the class add an admin action like:
     @admin.register(Book)
     class BookAdmin(models.ModelAdmin):
         save_as = True
    

    and this will create a "Save as New" button in your admin panel to completely clone the model object with all it's related fields.

提交回复
热议问题