rav668
rav668

Reputation: 11

Django Admin actions don't work after redefining primary key of the model

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

Upvotes: 1

Views: 151

Answers (1)

叶同学
叶同学

Reputation: 903

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

Upvotes: 0

Related Questions