Pierangelo Orizio Pierangelo Orizio - 3 months ago 18
Python Question

duplicate events action in django admin

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...

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']


before I has try simply so:

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']


but I get no action.

So I've tried in this other way:

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']


and I get this error:
global name 'duplicate_event' is not defined

Answer

Your indentation level is wrong, it should be:

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"

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