I am creating a Django Rest Framework notification application that can notify MARK AS READ ALL using the PATCH API in the interface . How can I do a bulk data update to complete this task.
This serializer and viewet below is for PATCH of only one notification object, but I want to do all this with field notifications is_read = False
Edited in the correct way
My serializers:
class NotificationEditSerializer(ModelSerializer):
class Meta:
model = Notification
fields = (
'id',
'is_read'
)
My viewset:
from rest_framework.response import Response
class NotificationListAPIView(ReadOnlyModelViewSet):
queryset = Notification.objects.all()
permission_classes = [AllowAny]
serializer_class = NotificationEditSerializer
lookup_field = 'id'
@list_route(methods=['PATCH'])
def read_all(self, request):
qs = Notification.objects.filter(is_read=False)
qs.update(is_read=True)
serializer = self.get_serializer(qs, many=True)
return Response(serializer.data)
My URL:
from rest_framework import routers
router.register(r'notifications/read_all', NotificationListAPIView)
Thanks for @Bear Brown
Hope this helps!
source
share