def update(self, request, *args, **kwargs): """ 用户可以标记一个 notification 为已读或者未读。标记已读和未读都是对 notification 的一次更新操作,所以直接重载 update 的方法来实现。另外一种实现方法是用一个专属的 action: @action(methods=['POST'], detail=True, url_path='mark-as-read') def mark_as_read(self, request, *args, **kwargs): ... @action(methods=['POST'], detail=True, url_path='mark-as-unread') def mark_as_unread(self, request, *args, **kwargs): ... 两种方法都可以,我更偏好重载 update,因为更通用更 rest 一些, 而且 mark as unread 和 mark as read 可以公用一套逻辑。 """ serializer = NotificationSerializerForUpdate( instance=self.get_object(), data=request.data, ) if not serializer.is_valid(): return Response({ 'message': "Please check input", 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) notification = serializer.save() return Response( NotificationSerializer(notification).data, status=status.HTTP_200_OK, )
def update(self, request, *args, **kwargs): """ 用户可以标记一个 notification 为已读或者未读。标记已读和未读都是对notification 的更新操作,所以直接重载update方法来实现,这种实现方法更加rest。另外一种方法是用一个 专属的action然后用两个方法实现如下 @action(methods=['POST], detail=True, url_path='mark-as-read') def mark_as_read(self, request, *args, **kwargs): ... @action(methods=['POST], detail=True, url_path='mark-as-unread') def mark_as_unread(self, request, *args, **kwargs): ... 但是这种方法无法封装这两个方法公用的逻辑。 """ serializer = NotificationSerializerForUpdate( instance=self.get_object(), data=request.data, ) if not serializer.is_valid(): return Response( { 'message': 'Please check input', 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) notification = serializer.save() return Response( NotificationSerializer(notification).data, status=status.HTTP_200_OK, )
def update(self, request, *args, **kwargs): """ user can mark a notification as read/unread, this an update for notification so override update, another method use action @action(method=['POST], detail=True, url_path='mark-as-read') def mark_as_read(self, request, *args, **kwargs) ... @action(method=['POST], detail=True, url_path='mark-as-unread') def mark_as_unread(self, request, *args, **kwargs) ... both are OK. override update is more restful, mark as read/unread can share :param request: :param args: :param kwargs: :return: """ serializer = NotificationSerializerForUpdate( instance=self.get_object(), data=request.data, ) if not serializer.is_valid(): return Response({ 'message': "Please check input", 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) notification = serializer.save() return Response( NotificationSerializer(notification).data, status=status.HTTP_200_OK )
def update(self, request, *args, **kwargs): serializer = NotificationSerializerForUpdate( instance=self.get_object(), data=request.data, ) if not serializer.is_valid(): return Response({ 'message': "Please check input", 'errors': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) notification = serializer.save() return Response( NotificationSerializer(notification).data, status=status.HTTP_200_OK, )
def update(self, request, *args, **kwargs): notification = self.get_object() serializer = NotificationSerializerForUpdate( data=request.data, instance=notification, ) if not serializer.is_valid(): return Response( { 'success': False, 'message': 'Please check the input', 'error': serializer.errors, }, status=status.HTTP_400_BAD_REQUEST) updated_notification = serializer.save() return Response( { 'success': True, 'notification': NotificationSerializer(updated_notification).data, }, status=status.HTTP_200_OK)