class StockUserSerializer(serializers.ModelSerializer): stock = StockSerializer.StockSerializer() user = CoreSerializer.UserSerializer() class Meta: model = StockUser fields = ("pk", "stock", "user", "price_min", "price_max")
def post(self, request, *args, **kwargs): company = Company.objects.get(pk=request.session['current_company_id']) company.name = request.data['name'] company.save() return Response({ "user": serializers.UserSerializer( request.user, context=self.get_serializer_context()).data, })
def send_users_from_db_to_recombee(self): users_queryset = User.objects.all() requests_to_batch = [] for user in users_queryset: user_json = serializers.UserSerializer(user) user_details = serialized_json_to_dict(user_json.data) request = AddUser(user_details['id']) requests_to_batch.append(request) self.recombee_client.send(Batch(requests_to_batch)) print("finished sending batched users to recombee..")
def post(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance, data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response({ "user": serializers.UserSerializer( instance, context=self.get_serializer_context()).data, })
class PostCommentSerializer(serializers.ModelSerializer): user = UserSerializers.UserSerializer() replies = serializers.SerializerMethodField(read_only=True) class Meta: model = models.PostComment fields = "__all__" def get_replies(self, obj): replies = models.PostCommentReply.objects.filter(comment=obj) data = PostCommentReplySerializer(replies, many=True).data return data
def post(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) user = serializer.validated_data['user'] token, created = Token.objects.get_or_create(user=user) result_serializer = serializers.UserSerializer( instance=user, many=False, context={'request': request}) result = dict() result['user'] = result_serializer.data result['token'] = token.key return Response(result)
def post(self, request, *args, **kwarg): company_id = request.data['company_id'] user = request.user user.companies.remove(company_id) if request.session.get('current_company_id') == company_id: company = user.companies.first() request.session[ 'current_company_id'] = company.pk if company else None return Response({ "user": serializers.UserSerializer( user, context=self.get_serializer_context()).data, 'current_company_id': request.session.get('current_company_id'), })
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.validated_data company = user.companies.first() self.request.session[ 'current_company_id'] = company.pk if company else None return Response({ "user": serializers.UserSerializer( user, context=self.get_serializer_context()).data, "token": AuthToken.objects.create(user)[1], 'current_company_id': self.request.session['current_company_id'] })
class PostSerializer(serializers.ModelSerializer): user = UserSerializers.UserSerializer() category = CategorySerializer() all_tags = serializers.SerializerMethodField(read_only=True) images = serializers.SerializerMethodField(read_only=True) files = serializers.SerializerMethodField(read_only=True) reacts = serializers.SerializerMethodField(read_only=True) comments = serializers.SerializerMethodField(read_only=True) class Meta: model = models.Post fields = "__all__" def get_images(self, obj): images = models.PostImage.objects.filter(post=obj) data = PostImageSerializer(images, many=True).data return data def get_files(self, obj): files = models.PostFile.objects.filter(post=obj) data = PostFileSerializer(files, many=True).data return data def get_reacts(self, obj): reacts = models.PostReact.objects.filter(post=obj) data = PostReactSerializer(reacts, many=True).data return data def get_comments(self, obj): comments = models.PostComment.objects.filter(post=obj) data = PostCommentSerializer(comments, many=True).data return data def get_all_tags(self, obj): tags = obj.tags.all() data = TagSerializer(tags, many=True).data return data
def post(self, request, *args, **kwargs): user_data = serializers.UserSerializer(request.user).data return Response(user_data)
class PostCommentReplySerializer(serializers.ModelSerializer): user = UserSerializers.UserSerializer() class Meta: model = models.PostCommentReply fields = "__all__"
def retrieve(self, request, pk=None): if request.user.is_authenticated(): return Response(serializers.UserSerializer(request.user).data) raise NotAuthenticated()
def post(self, request): ser = serializers.UserSerializer(data=request.data) ser.is_valid(raise_exception=True) ser.save() return Response(data=ser.data, status=status.HTTP_201_CREATED)
def get(self, request): queryset = User.objects.all() print(queryset) ser = serializers.UserSerializer(data=queryset, many=True) ser.is_valid() return Response(data=ser.data, status=status.HTTP_200_OK)