duplicate events action in django admin
<p>I have a duplicate records function in my django admin.py, and in someway it works, but the weird things is that i have must duplicate this function outside and inside the modelAdmin... </p>

<pre><code>def duplicate_event(ModelAdmin, request, queryset):
    for object in queryset:
        object.id = None
        object.save()
    duplicate_event.short_description = "Duplicate selected record"

class ProductAdmin(ImageCroppingMixin, admin.ModelAdmin):
    model = Product
    inlines = [CompositionAssociactionAdmin]
    list_display = ("image_img", "code", "name", "price", "discount", "price_offer", "prompt_delivery", "delivery", "promo", "active")
    list_editable = ('active',)
    fields = (
                ("name", "code"), 
                ("price", "discount", "price_offer"),
                ("color", "material"),
                ("scarpemisura", "cintureLunghezza"),
                "size",
                ("width", "lenght", "depth", "height"),
                "volume",
                "descrizione", "album",
                "image", "slider", "thumb", "thumbdue", "croplibero",
                ("prompt_delivery", "delivery"),
                ("slide", "promo"),
                "tags", "active", "pub_date"
            )
    def duplicate_event(ModelAdmin, request, queryset):
        for object in queryset:
            object.id = None
            object.save()
        duplicate_event.short_description = "Duplica Record Selezionati"

    actions = ['duplicate_event']
</code></pre>

<p>before I has try simply so:</p>

<pre><code>def duplicate_event(ModelAdmin, request, queryset):
    for object in queryset:
        object.id = None
        object.save()
    duplicate_event.short_description = "Duplicate selected record"

class ProductAdmin(ImageCroppingMixin, admin.ModelAdmin):
    model = Product
    inlines = [CompositionAssociactionAdmin]
    list_display = ("image_img", "code", "name", "price", "discount",  "price_offer", "prompt_delivery", "delivery", "promo", "active")
    list_editable = ('active',)
    fields = (
                ("name", "code"), 
                ("price", "discount", "price_offer"),
                ("color", "material"),
                ("scarpemisura", "cintureLunghezza"),
                "size",
                ("width", "lenght", "depth", "height"),
                "volume",
                "descrizione", "album",
                "image", "slider", "thumb", "thumbdue", "croplibero",
                ("prompt_delivery", "delivery"),
                ("slide", "promo"),
                "tags", "active", "pub_date"
            )

    actions = ['duplicate_event']
</code></pre>

<p>but I get no action.</p>

<p>So I've tried in this other way:</p>

<pre><code>class ProductAdmin(ImageCroppingMixin, admin.ModelAdmin):
    model = Product
    inlines = [CompositionAssociactionAdmin]
    list_display = ("image_img", "code", "name", "price", "discount", "price_offer", "prompt_delivery", "delivery", "promo", "active")
    list_editable = ('active',)
    fields = (
                ("name", "code"), 
                ("price", "discount", "price_offer"),
                ("color", "material"),
                ("scarpemisura", "cintureLunghezza"),
                "size",
                ("width", "lenght", "depth", "height"),
                "volume",
                "descrizione", "album",
                "image", "slider", "thumb", "thumbdue", "croplibero",
                ("prompt_delivery", "delivery"),
                ("slide", "promo"),
                "tags", "active", "pub_date"
            )

    def duplicate_event(ModelAdmin, request, queryset):
        for object in queryset:
            object.id = None
            object.save()
        duplicate_event.short_description = "Duplica Record Selezionati"

    actions = ['duplicate_event']
</code></pre>

<p>and I get this error:
<strong>global name 'duplicate_event' is not defined</strong></p>

<p>Your indentation level is wrong, it should be:</p>

<pre><code>class ProductAdmin(ImageCroppingMixin, admin.ModelAdmin):
    def duplicate_event(ModelAdmin, request, queryset):
        for object in queryset:
            object.id = None
            object.save()
    duplicate_event.short_description = "Duplica Record Selezionati"
</code></pre>

<p>The last line, which assigns <code>duplicate_event.short_description</code>, should be part of the class body, not part of the method body. The global function is unnecessary. </p>

python django backend