3

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?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • how about by putting your function of `duplicate` inside `ProductAdmin` with `def duplicate(self, request, queryset):` also `duplicate.allow_tags = True`? and the `actions = [ 'duplicate', ]` – binpy Feb 21 '17 at 18:06
  • @SancaKembang This didn't help unfortunately. – Milano Feb 22 '17 at 12:53
  • You can't delete the duplicated products or any of them? – nik_m Mar 05 '17 at 20:07

1 Answers1

4

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)
Akshar Raaj
  • 14,231
  • 7
  • 51
  • 45