I am building a simple Photos-Albums app with Django Rest Framework (DRF). I would like to be able to delete multiple albums at once by supplying an array of ids. I am using viewsets.ModelViewSet for the basic views.
class AlbumViewSet(viewsets.ModelViewSet):
queryset = Album.objects.all()
serializer_class = AlbumSerializer
I have managed to create a view to show the albums before the delete operation by adding this function to my views.py file.
@api_view(['GET', 'DELETE'])
def delete_albums(request):
"""
GET: list all albums
DELETE: delete multiple albums
"""
if request.method == 'GET':
albums = Album.objects.all()
serializer = AlbumSerializer(albums, many=True)
return Response(serializer.data)
elif request.method == 'DELETE':
ids = request.data
albums = Album.objects.filter(id__in=ids)
for album in albums:
album.delete()
serializer = AlbumSerializer(albums, many=True)
return Response(serializer.data)
If I run curl -X delete -H "Content-Type: application/json" http://localhost:8000/albums/delete -d '{"data":[1,2,3]}' then it will delete albums with ids 1,2,3.
This is OK, except that:
- It's not class-based, and I'd prefer to have everything class-based if possible
- I would prefer to have a form in the view that lets me input an array e.g. [1,2,3], hit a delete button, and then see the results of the query in the browser, much like when one posts a new object through the interface.
Can anyone please outline how to achieve that? Thanks!