I
I
ixotep2019-09-26 19:57:39
Django
ixotep, 2019-09-26 19:57:39

How to copy a model object in Django?

Good afternoon!
Tell me how you can implement copying / cloning of a Django model object in the Admin panel?
Maybe somehow you can add an action to the context menu where "Delete selected objects" ..??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
latush, 2019-09-26
@ixotep

everything is in the official documentation https://docs.djangoproject.com/en/2.2/ref/contrib/...

Y
Yuri Iovkov, 2020-08-29
@Rurick80

Yes. Very simple:
Here is my example:

def dublicate_ad(modeladmin, request, queryset):
    #клонирование выбранных Ad
    for ad in queryset:
        ad.pk = None
        ad.save()

dublicate_ad.short_description = "Дублировать объект"    

@admin.register(Ad)
class AdAdmin(admin.ModelAdmin):
    actions = [dublicate_ad]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question