Django – 将序列化器的输出传递给另一个序列化器。

huangapple go评论52阅读模式
英文:

django - get serializer output into another serializer

问题

我想将一个序列化器 'RoomSerializer' 的输出放入 'JoinRoomSerializer' 的字段中。

我尝试将 'RoomSerializer' 作为一个变量传递给 'JoinRoomSerializer',然后在 'fields' 中传递它,但是我得到了以下错误:在序列化器 'JoinedRoomSerializer' 上尝试获取字段 'room_extends' 的值时出现了 AttributeError。
序列化器字段的命名可能不正确,与 'UserRoom' 实例上的任何属性或键不匹配。
原始异常文本为:'UserRoom' 对象没有属性 'room_extends'。

Room 模型有一个字段名,JoinRoom 模型有一个对 room 的外键。

我的序列化器:

class RoomSerializer(serializers.ModelSerializer):
    """Serializer for displaying all fields of the Room"""

    class Meta():
        model = Room
        fields = ['name']


class JoinedRoomSerializer(serializers.ModelSerializer):
    """Serializer for displaying the following fields of the joined UserRoom table"""
    room_extends = RoomSerializer(many=True)

    class Meta():
        model = UserRoom
        fields = ['room_extends', 'date_joined']
英文:

I want to get the output of one serializer 'RoomSerializer' into 'JoinRoomSerializer's fields.

I have tried to get the 'RoomSerializer' as a variable into 'JoinRoomSerializer' and then pass it in 'fields', but im getting the following error: Got AttributeError when attempting to get a value for field room_extends on serializer JoinedRoomSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the UserRoom instance.
Original exception text was: 'UserRoom' object has no attribute 'room_extends'.

The Room model has field name and JoinRoom model has foreign key to room

My serializers:

class RoomSerializer(serializers.ModelSerializer):
    """Serializer for displaying all fields of the Room"""

    class Meta():
        model = Room
        fields = ['name']


class JoinedRoomSerializer(serializers.ModelSerializer):
    """Serializer for displaying the following fields of the joined UserRoom table"""
    room_extends = RoomSerializer(many=True)

    class Meta():
        model = UserRoom
        fields = ['room_extends', 'date_joined']

答案1

得分: 0

你正在使用ModelSerializer,所以在Meta类中,你必须指定模型拥有的字段。我几乎可以确定,在你的情况下,外键字段的名称不是room_extends,这导致了错误。

class RoomSerializer(serializers.ModelSerializer):
    """用于显示Room所有字段的序列化器"""

    class Meta:
        model = Room
        fields = ['name']


class JoinedRoomSerializer(serializers.ModelSerializer):
    """用于显示已加入的UserRoom表的以下字段的序列化器"""
    room = RoomSerializer(many=True)  # 变量名与UserRoom模型中的相同

    class Meta:
        model = UserRoom
        fields = ['room', 'date_joined']

或者你可以使用source

class RoomSerializer(serializers.ModelSerializer):
    """用于显示Room所有字段的序列化器"""

    class Meta:
        model = Room
        fields = ['name']


class JoinedRoomSerializer(serializers.ModelSerializer):
    """用于显示已加入的UserRoom表的以下字段的序列化器"""
    room_extends = RoomSerializer(many=True, source='room')  # room或者是外键字段的名称

    class Meta:
        model = UserRoom
        fields = ['room_extends', 'date_joined']
英文:

You are using ModelSerializer so you in Meta class you have to specify fields that your model has. I am almost sure in your case foreign key field is not named room_extends and this cases the error

class RoomSerializer(serializers.ModelSerializer):
    """Serializer for displaying all fields of the Room"""

    class Meta():
        model = Room
        fields = ['name']


class JoinedRoomSerializer(serializers.ModelSerializer):
    """Serializer for displaying the following fields of the joined UserRoom table"""
    room = RoomSerializer(many=True) # name of variable same as in UserRoom model

    class Meta():
        model = UserRoom
        fields = ['room', 'date_joined']

Or you can use source

class RoomSerializer(serializers.ModelSerializer):
    """Serializer for displaying all fields of the Room"""

    class Meta():
        model = Room
        fields = ['name']


class JoinedRoomSerializer(serializers.ModelSerializer):
    """Serializer for displaying the following fields of the joined UserRoom table"""
    room_extends = RoomSerializer(many=True, source='room') # room or whatever is foreignkey field name

    class Meta():
        model = UserRoom
        fields = ['room_extends', 'date_joined']

huangapple
  • 本文由 发表于 2023年8月8日 19:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定