Python 如何在django中自定义onetomany关系的序列化程序数据

Python 如何在django中自定义onetomany关系的序列化程序数据,python,django,django-rest-framework,Python,Django,Django Rest Framework,我为图像模型定义序列化程序,并在调用后序列化程序时调用它以立即提供数据。这个操作没有问题,但我并没有按照我的要求提供json数据 现在我把它给你,就这样 { "pk": 0, "author": { "id": 0, "email": "", "username": &quo

我为图像模型定义序列化程序,并在调用后序列化程序时调用它以立即提供数据。这个操作没有问题,但我并没有按照我的要求提供json数据

现在我把它给你,就这样

{
        "pk": 0,
        "author": {
            "id": 0,
            "email": "",
            "username": "",
            "profile": "url",
            "following": 0,
            "followers": 0
        },
        "title": "",
        "text": "",
        "view": 0,
        "images": [
            {
                "image": "url"
            },
            {
                "image": "url"
            },
            {
                "image": "url"
            },
            {
                "image": "url"
            }
        ],
        "like_count": 0,
        "comment_count": 0,
        "liker": [
            0,
            0
        ],
        "tag": null,
        "created_at": "2020-10-06T21:46:48.039354+09:00"
    },
我想要这个

{
            "pk": 0,
            "author": {
                "id": 0,
                "email": "",
                "username": "",
                "profile": "url",
                "following": 0,
                "followers": 0
            },
            "title": "",
            "text": "",
            "view": 0,
            "images": [
                url,
                url,
                url,
                url
            ],
            "like_count": 0,
            "comment_count": 0,
            "liker": [
                0,
                0
            ],
            "tag": null,
            "created_at": "2020-10-06T21:46:48.039354+09:00"
        },
json将如何以我想要的方式出现?这是我的密码

序列化程序.py

class ImageSerializer (serializers.ModelSerializer) :
    image = serializers.ImageField(use_url=True)

    class Meta :
        model = Image
        fields = ('image', )

class CommentSerializer (serializers.ModelSerializer) :
    comments_author = userProfileSerializer(read_only=True)

    class Meta :
        model = Comment
        fields = ('pk', 'comments_author', 'text', 'created_at')

    def create (self, validated_data) :
        return Comment.objects.create(**validated_data)

    def validate (self, attrs) :
        text = attrs.get('text', '')

        error = {}

        if text is None :
            error['message'] = '본문은 빈칸일 수 없습니다.'
            raise serializers.ValidationError(error)

        return attrs

class LikeSerializer (serializers.ModelSerializer) :

    class Meta :
        model = Like
        fields = '__all__'

class LikerSerializer (serializers.ModelSerializer) :
    id = serializers.IntegerField(source='liker.pk')

    class Meta :
        model = Like
        fields = ('id', )

class PostSerializer (serializers.ModelSerializer) :
    author = userProfileSerializer(read_only=True)
    like_count = serializers.ReadOnlyField()
    comment_count = serializers.ReadOnlyField()
    images = ImageSerializer(many=True, read_only=True)
    liker = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta :
        model = Post
        fields = ('pk', 'author', 'title', 'text', 'view', 'images', 'like_count', 'comment_count', 'liker', 'tag', 'created_at')

    def create (self, validated_data) :
        images_data = self.context['request'].FILES
        post = Post.objects.create(**validated_data)

        for image_data in images_data.getlist('image') :
            Image.objects.create(post=post, image=image_data)

        return post

    def validate (self, attrs) :
        title = attrs.get('title', '')
        text = attrs.get('text', '')

        error = {}

        if title is None and text is None :
            error['message'] = '제목과 본문은 빈칸일 수 없습니다.'
            raise serializers.ValidationError(error)

        if title is None :
            error['message'] = '제목은 빈칸일 수 없습니다.'
            raise serializer.ValidationError(error)

        if text is None :
            error['message'] = '본문은 빈칸일 수 없습니다.'    
            raise serializer.ValidationError(error)

        return attrs
您希望在
后序列化程序中使用而不是
图像序列化程序

类后序列化程序(serializers.ModelSerializer):
...
images=serializers.SlugRelatedField(many=True,read\u only=True,slug\u field='image'))
要在
后序列化程序中使用而不是
图像序列化程序

类后序列化程序(serializers.ModelSerializer):
...
images=serializers.SlugRelatedField(many=True,read\u only=True,slug\u field='image'))

我尝试了,但出现了错误“Image”对象没有属性“url”我的答案被编辑,
slug\u字段
应该是“Image”我尝试了,但出现了错误“Image”对象没有属性“url”我的答案被编辑,
slug\u字段
应该是“Image”