def create(self, request): serializer = TweetSerializerForCreate( data=request.data, context={'request': request}, ) if not serializer.is_valid(): return Response({ "success": False, "message": "Please check input.", "errors": serializer.errors, }, status=400) # save will call create method in TweetSerializerForCreate tweet = serializer.save() NewsFeedService.fanout_to_followers(tweet) return Response(TweetSerializer(tweet).data, status=201)
def create(self, request): serializer = TweetSerializerForCreate( data = request.data, context={'request': request} ) if not serializer.is_valid(): return Response({ 'success': False, 'message': 'Please check input.', 'errors': serializer.errors, }, status=400 ) tweet = serializer.save() NewsFeedService.fanout_to_followers(tweet) return Response(TweetSerializer(tweet, context={'request':request}).data , status=201)
def create(self, request): serialzier = TweetSerializerForCreate( data=request.data, context={'request': request}, ) if not serialzier.is_valid(): return Response( { "success": False, "message": "Please check input.", "errors": serialzier.errors, }, status=HTTP_400_BAD_REQUEST) tweet = serialzier.save() #fan out to followers NewsFeedService.fan_out_to_followers(tweet) return Response(TweetSerializer(tweet).data, status=HTTP_201_CREATED)
def create(self, request): """ 重载 create 方法,因为需要默认用当前登录用户作为 tweet.user """ serializer = TweetSerializerForCreate( data=request.data, context={'request': request}, ) if not serializer.is_valid(): return Response({ 'success': False, 'message': "Please check input", 'errors': serializer.errors, }, status=400) # save() will call create method in TweetCreateSerializer tweet = serializer.save() NewsFeedService.fanout_to_followers(tweet) serializer = TweetSerializer(tweet, context={'request': request}) return Response(serializer.data, status=201)
def create(self, request): serializer = TweetSerializerForCreate( data=request.data, context={'request': request}, ) if not serializer.is_valid(): return Response({ "success": False, "message": "Please check input", "errors": serializer.errors, }, status=400) # otherwise we have a tweet instance here # save() will call create method in TweetSerializerForCreate tweet = serializer.save() NewsFeedService.fanout_to_followers(tweet) # Here I use TweetSerializer to show whats in the tweet # reminder: It is different from TweetSerializerForCreate return Response( TweetSerializer(tweet, context={'request': request}).data, status=201, )
def create(self, request, *args, **kwargs): """ overload create function, to default the login user as a tweet.user """ serializer = TweetSerializerForCreate( data=request.data, context={'request': request}, ) if not serializer.is_valid(): return Response( { 'success': False, 'message': 'Please check input', 'errors': serializer.errors, }, status=400) tweet = serializer.save() NewsFeedService.fanout_to_followers(tweet) serializer = TweetSerializer(tweet, context={'request': request}) return Response(serializer.data, status=201)
def create(self, request, *args, **kwargs): """ overload create method, need current logged in user as tweet.user :param request: :param args: :param kwargs: :return: """ serializer = TweetSerializerForCreate( data=request.data, context={'request': request}, ) if not serializer.is_valid(): return Response({ 'success': False, 'message': 'Please check input', 'error': serializer.errors, }, status=400) tweet = serializer.save() NewsFeedService.fanout_to_followers(tweet) serializer = TweetSerializer(tweet, context={'request': request}) return Response(serializer.data, status=201)
def create(self, request): serializer = TweetSerializerForCreate( data=request.data, context={'request': request}, ) if not serializer.is_valid(): return Response( { "success": False, "message": "Please check input.", "errors": serializer.errors, }, status=400) # save will trigger create method in TweetSerializerForCreate tweet = serializer.save() # fanout这个方法需要较复杂的逻辑实现,而在view这一层中尽量做一些简单的显示视图的功能 # 对于复杂的逻辑实现放到service这一层中去提供 NewsFeedService.fanout_to_followers(tweet) return Response( TweetSerializer(tweet, context={ 'request': request }).data, status=201, )