class ThreadDetailSerializer(serializers.ModelSerializer): messages = ChatMessageSerializer(many=True, read_only=True) first = UsersListSerializer(many=False, read_only=True) second = UsersListSerializer(many=False, read_only=True) class Meta: model = Thread fields = [ 'id', 'first', 'second', 'time', 'access', 'times_rooms', 'timestamp', 'messages' ] read_only_fields = [ 'first', 'second', 'time', 'times_rooms', 'timestamp', 'messages' ]
class ThreadListSerializer(serializers.ModelSerializer): first = UsersListSerializer(many=False, read_only=True) second = UsersListSerializer(many=False, read_only=True) # last_message = serializers.CharField() class Meta: model = Thread fields = [ 'id', 'first', 'second', 'time', 'access', 'times_rooms', 'timestamp' ] read_only_fields = [ 'first', 'second', 'time', 'times_rooms', 'timestamp' ]
class CommentSerializer(serializers.ModelSerializer): user = UsersListSerializer(read_only=True, many=False) class Meta: model = Comment fields = ['id', 'user', 'forum', 'description', 'pub_date'] read_only_fields = ['user', 'pub_date']
def get(self, request): all_users = User.objects.all() paginator = PageNumberPagination() # paginamos el queryset paginated_users = paginator.paginate_queryset(all_users, request) serializer = UsersListSerializer(paginated_users, many=True) return paginator.get_paginated_response(serializer.data)
def list(self, request): """ Returns a list of the system users :param request: HttpRequest :return: Response """ users = User.objects.all().values("id", "username") page = self.paginate_queryset(users) serializer = UsersListSerializer(page, many=True) return self.get_paginated_response(serializer.data)
def list(self, request): """ Returns a list of the system users :param request: HttpRequest :return: Response """ users = User.objects.all().values("id", "username") serializer = UsersListSerializer(users, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
class ForumListSerializer(serializers.ModelSerializer): user = UsersListSerializer(many=False, read_only=True) comment_count = serializers.IntegerField() class Meta: model = Forum fields = [ 'id', 'user', 'category', 'subcategory', 'types', 'subtypes', 'title', 'pub_date', 'comment_count' ] read_only_fields = ['user', 'pub_date']
class ArticleListSerializer(serializers.ModelSerializer): additions = ArticleAdditionSerializer(read_only=True, many=True) votes = serializers.IntegerField(source='total_votes', read_only=True) user = UsersListSerializer(read_only=True, many=False) class Meta: model = Article fields = [ 'id', 'user', 'category', 'subcategory', 'types', 'subtypes', 'title', 'text', 'additions', 'pub_date', 'status', 'votes' ] read_only_fields = ['user', 'pub_date', 'votes', 'status', 'additions']
class VoteSerializer(serializers.ModelSerializer): user = UsersListSerializer(read_only=True, many=False) article = serializers.PrimaryKeyRelatedField(read_only=True) class Meta: model = Vote fields = ['id', 'user', 'vote', 'article', 'pub_date'] read_only_fields = ['user', 'pub_date'] def create(self, validated_data): vote, _ = Vote.objects.update_or_create( user=validated_data.get('user', None), article=validated_data.get('article', None), defaults={'vote': validated_data.get('vote')}) return vote
class ArticleDetailSerializer(serializers.ModelSerializer): additions = ArticleAdditionSerializer(many=True) votes = serializers.IntegerField(source='total_votes', read_only=True) user = UsersListSerializer(read_only=True, many=False) class Meta: model = Article fields = [ 'id', 'user', 'category', 'subcategory', 'types', 'subtypes', 'title', 'text', 'additions', 'pub_date', 'status', 'votes' ] read_only_fields = ['user', 'pub_date', 'votes', 'status'] def create(self, validated_data): additions_data = validated_data.pop('additions') article = Article.objects.create(**validated_data) for addition_data in additions_data: ArticleAddition.objects.create(article=article, **addition_data) return article
def friends(request): user = request.user serializer = UsersListSerializer(user.friends, many=True) return Response(serializer.data)
def user_list(request): all_users = User.objects.all() serializer = UsersListSerializer(all_users, many=True) return Response(serializer.data)
def get(self, request): users = User.objects.all() # users es un objeto que hay que convertir al formato de salida serializer = UsersListSerializer(users, many=True) # El serializador obtiene un diccionario por cada usuario de la lista return Response(serializer.data)