我创建了一个包含几个字段的简单模型产品,然后转到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
你知道问题出在哪里吗?
答案 0 :(得分:1)
您应在自定义表单字段中添加required=False
。之后一切都会按预期工作。
class DuplicateActionForm(ActionForm):
number = forms.IntegerField(required=False)