[Answered ]-Django api using generic views for deleting multiple objects

1👍

The get_object_or_404 method only gets 1 object from the table so it is compulsory the record will be unique in the table. Otherwise, it gives an error.

Please try this code:

  def delete(self, request, *args, **kwargs):
        employees = Employee.objects.filter(gender=kwargs['gender'])
        if employees.count() > 0:
           employees.delete()
           return Response("Femails deleted", status=status.HTTP_204_NO_CONTENT)
        return Response("Unable to find the femails.", status=status.HTTP_404_OK)

In this code snippet, I am filtering the result based on kwargs[‘gender’] and then count its objects if greater than one then delete it using the loop.

Leave a comment