"未选择任何操作" Django-admin在删除对象时说

时间:2017-02-21 17:36:53

标签: python django django-admin django-1.10

我创建了一个包含几个字段的简单模型产品,然后转到admin.py。我已注册Product,制作了一些字段list_editable并创建了新的操作duplicate

def duplicate(modeladmin, request, queryset):
    number = int(request.POST['number'])
    product = queryset.first()
    for i in xrange(number):
        product.id = None
        product.save()

class DuplicateActionForm(ActionForm):
    number = forms.IntegerField()

class ProductAdmin(admin.ModelAdmin):
    list_display = ('id','name','color','memory','ga_url','gs_url',)
    list_editable = ('color','memory','name','ga_url','gs_url',)
    action_form = DuplicateActionForm
    # actions = [duplicate,]

admin.site.register(Product,ProductAdmin)

actions类的ProductAdmin属性未被注释时,我可以复制对象。问题是我无法删除它们。当我检查行并选择delete selected时,它会显示:No action selected

这是由行:

引起的
action_form = DuplicateActionForm

因为如果actions = [duplicate,]被评论,我无法正确删除对象,直到我发表评论action_form = DuplicateActionForm

你知道问题出在哪里吗?

1 个答案:

答案 0 :(得分:1)

您应在自定义表单字段中添加required=False。之后一切都会按预期工作。

class DuplicateActionForm(ActionForm):
    number = forms.IntegerField(required=False)