def create(self, request, *args, **kwargs): try: data = self.take_data_from_request(request) serializer = PostSerializer(data=data) serializer.is_valid(raise_exception=True) post = serializer.save() post_data = post.__dict__ if '_state' in post_data: del post_data['_state'] return Response(post_data, status=status.HTTP_200_OK) except Exception as exception: raise exception
def list(self, request): try: queryset = Posts.objects.all().filter(user_id=request.user.id) serializer = PostSerializer(queryset, many=True) return Response(serializer.data) except Exception as exception: raise exception
def retrieve(self, request, pk=None, **kwargs): try: post = self.get_and_check(pk) serializer = PostSerializer(post) return Response(serializer.data) except Exception as exception: raise exception
def get_list_post_of_friend(self, request): try: get_friend = Friend.objects.all().filter(user_id=1) for i in get_friend: queryset = Posts.objects.all().filter(user_id=i.friend_user_id) serializer = PostSerializer(queryset, many=True) return Response(serializer.data) except Exception as exception: raise exception