1

I am using standard models.Model in Django. I changed my primary key to _id (mongo). After that, actions in Django Admin don't work anymore. For example, when I use the default Django action – Delete selected, no object is found through the query. However, no problem occurs when I edit or delete objects manually. Is there any way to fix it?

Are you sure you want to delete the selected companies? -> Summary, objects: None

rav668
  • 11
  • 3

1 Answers1

0
selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)

selected is list of string, You should convert to bson.ObjectId.

So queryset = queryset.filter(pk__in=selected) will get the right result.

Also you can override the function def response_action(self, request, queryset): of admin.Model class to edit the code as this:

            selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)
            try:
                from bson import ObjectId
                selected = [ObjectId(item) for item in selected]
            except Exception as e:
                pass

code image

叶同学
  • 801
  • 1
  • 9
  • 21