简体   繁体   中英

“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 . I've registered the Product , make some fields list_editable and created a new action 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. The problem is that I can't delete them. When I check row and select delete selected , it says: 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

Do you know where is the problem?

You should add required=False on your custom form field. After that everything would work as expected.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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