1

I'm working on small project using Django Rest Framework, i would like to delete multiple IDs but i get always an error when i send a delete request by sending IDs /1,2,3,4 as a string, i get id must be an integer.

this is my code,

class UpdateDeleteContact(APIView):

def get(self, request, pk):
    contactObject = get_object_or_404(Contact, pk=pk)
    serializeContactObject = ContactSerializer(contactObject)
    return Response(serializeContactObject.data)
    

def delete(self, request, pk):
    delete_id = request.get('deleteid', None)
    if not delete_id:
        return Response(status=status.HTTP_404_NOT_FOUND)
    for i in delete_id.split(','):
        get_object_or_404(User, pk=int(i)).delete()
    return Response(status=status.HTTP_204_NO_CONTENT)

can someone give me an example how to bulk delete

Amine Bouhaddi
  • 554
  • 1
  • 7
  • 24

2 Answers2

4

This code will enable you to send multiple ids through delete method and receive them as string.

    path('url/<str:pk_ids>/', views.UpdateDeleteContact.as_view()),
#view.py
class UpdateDeleteContact(APIView):
    def get(self, request, pk_ids):
        ids = [int(pk) for pk in pk_ids.split(',')]
        contactObject = Contact.objects.filter(id__in=ids)
        serializeContactObject = ContactSerializer(contactObject, many=True)
        return Response(serializeContactObject.data)

    def delete(self, request, pk_ids):
        ids = [int(pk) for pk in pk_ids.split(',')]
        for i in ids:
            get_object_or_404(User, pk=i).delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

But for me it is not recommended since the defined url can interfere with other methods like retrieve.

Another solution that I can offer you is to enter a parameter in the url, let's call it "pk_ids", with the different ids separated by commas.

def delete(self, request, pk):
    pk_ids = request.query_params.get('pk_ids', None)
    if pk_ids:
        for i in pk_ids.split(','):
            get_object_or_404(User, pk=int(i)).delete()
    else:
        get_object_or_404(User, pk=pk).delete()
    return Response(status=status.HTTP_204_NO_CONTENT)

So you should call the url like url.com/url/?pk_ids=1,2,3,4

Tiki
  • 760
  • 1
  • 8
  • 16
  • pk_fields could be sent through the body, I've searched and there is nothing against sending payload in DELETE request, and in some cases it could be even more flexible to do that. https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request – Seyed Mostafa SeyedAshoor Jul 24 '22 at 04:34
  • @SeyedMostafaSeyedAshoor, have you found a way to send a payload in the body when issuing delete request? – ForMartha Jul 31 '23 at 19:20
0

Try with:

User.objects.filter(id__in=[int(x) for x in delete_id.split(',') if x]).delete()

New, more explained...
Your code doesn't work like a restful API... Delete is only for one object as a rule. You can read this thread

As Sohaib said in the comments, you can use the post function and retrieve id list from body, or as a string like your example.

kaajavi
  • 560
  • 4
  • 11