django admin中的重复事件操作

时间:2016-09-06 22:15:50

标签: python django backend

我的django admin.py中有一个重复的记录功能,并且在某种程度上它可以工作,但奇怪的是我必须在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']

在我尝试之前:

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

但我没有采取行动。

所以我以其他方式尝试过:

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

我收到此错误: 全球名称' duplicate_event'未定义

1 个答案:

答案 0 :(得分:0)

您的缩进级别错误,应该是:

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"

指定duplicate_event.short_description的最后一行应该是类主体的一部分,而不是方法主体的一部分。全局功能是不必要的。