Create a Django Admin Action to Duplicate a Record

后端 未结 2 378
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 21:13

I want to create a Django Admin Action that allows me to create a duplicate of a record.

Heres the use case.

Admin clicks the checkbox next to a record in an

相关标签:
2条回答
  • 2021-01-02 21:28

    You have the right idea but you need to iterate through the queryset then duplicate each object.

    def duplicate_event(modeladmin, request, queryset):
        for object in queryset:
            object.id = None
            object.save()
    duplicate_event.short_description = "Duplicate selected record"
    
    0 讨论(0)
  • 2021-01-02 21:50

    Maybe this work to for you.

    def duplicate_query_sets(queryset, **kwargs):
        for p in queryset:
            p.pk = None
            for i, v in kwargs.iteritems():
                setattr(p, i, v)
    
            p.save()
    
    0 讨论(0)
提交回复
热议问题