django DRF不可写嵌套序列化程序相关字段

dphi5xsq  于 2023-04-07  发布在  Go
关注(0)|答案(4)|浏览(148)

我正在使用Django 3.2和DRF
我有一个序列化程序,它具有嵌套的序列化程序字段,如

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = [
            'id',
            'title',
        ]

class QuestionSerializer(serializers.ModelSerializer):

    category = CategorySerializer()

    class Meta:
        model = Question
        fields = [
            'category',
            'title',
            'description'
        ]

在响应中,数据应该像这样返回

{
  "category": {
    "id": 4,
    "title": "General Knowledge",
    "created": "2021-05-23T07:12:14.749571Z",
    "modified": "2021-05-23T07:12:14.749639Z"
  },
  "title": "Where is Delhi?",
  "description": ""
}

这很好,但是使用POST方法,我不想为Category创建嵌套数据,因为这些记录只能从管理面板创建。
但是这些字段是创建Question记录所必需的。
当在payload中传递类别的id时,它仍然说需要category字段。

如何使用对象的id来分配相关字段,而不是传递所有数据来创建嵌套对象?

h7wcgrx3

h7wcgrx31#

class QuestionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Question
        fields = [
            'category',
            'title',
            'description',
        ]

    def to_representation(self, instance):
        data = super().to_representation(instance)
        data['category'] = CategorySerializer(
            Category.objects.get(pk=data['category'])).data
        return data

试试看

p4tfgftt

p4tfgftt2#

请尝试添加单独的序列化程序以创建问题:

class QuestionListSerializer(serializers.ModelSerializer):

    class Meta:
        model = Question
        fields = [
            'category',
            'title',
            'description'
        ]
yyyllmsg

yyyllmsg3#

您也可以将该字段设置为不需要:

class QuestionSerializer(serializers.ModelSerializer):
    category = CategorySerializer(required=False)
xjreopfe

xjreopfe4#

为了使用现有类别创建问题,客户端必须知道category_id

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = [
            'id',
            'title',
        ]

class QuestionSerializer(serializers.ModelSerializer):

    category = CategorySerializer(read_only=True)
    category_id = serializers.PrimaryKeyRelatedField(
        queryset=Category.objects.all(), source="category", write_only=True
    )  # You can add required=True, if you want to require this field on creation

    class Meta:
        model = Question
        fields = [
            'category',
            'category_id',
            'title',
            'description'
        ]

在这种情况下,category只在GET请求中可见(例如QuestionSerializer(instance).data)。
category_id可以传递给POST请求。
request.data

{
    "category_id": 4,
    "title": "title1",
    "description": "description1"

}
serializer = QuestionSerializer(data=request.data)
serializer.is_valid()
serializer.save()
serializer.data

创建后的响应(serializer.data)将是:

{
    "category": {
        "id": 4,
        "title": "General Knowledge",
        "created": "2021-05-23T07:12:14.749571Z",
        "modified": "2021-05-23T07:12:14.749639Z"
    },
    "title": "title1",
    "description": "description1"
}

相关问题