简体   繁体   English

“没有选择任何行动”Django-admin在删除对象时说

[英]“No action selected” says Django-admin when deleting objects

I've created a simple model Product with couple of fields and then went to admin.py . 我创建了一个带有几个字段的简单模型产品,然后转到admin.py I've registered the Product , make some fields list_editable and created a new action duplicate . 我已经注册了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)

When actions attribute of ProductAdmin class is not commented, I can duplicate objects. 当没有评论ProductAdmin类的actions属性时,我可以复制对象。 The problem is that I can't delete them. 问题是我无法删除它们。 When I check row and select delete selected , it says: No action selected . 当我检查行并选择delete selected ,它会显示:未No action selected

This is caused by line: 这是由线引起的:

action_form = DuplicateActionForm

because if actions = [duplicate,] is commented, I can't delete objects correctly until I comment action_form = DuplicateActionForm 因为如果actions = [duplicate,]被注释,我就无法正确删除对象,直到我发表评论action_form = DuplicateActionForm

Do you know where is the problem? 你知道问题出在哪里吗?

You should add required=False on your custom form field. 您应该在自定义表单字段中添加required=False After that everything would work as expected. 之后,一切都会按预期工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM