【问题标题】:Django-rest-framework: disable nested fieldsDjango-rest-framework:禁用嵌套字段
【发布时间】:2015-11-07 15:46:39
【问题描述】:

我正在使用 Django REST Framework 和我

我有一个名为 QuestionSerializer 的序列化程序,我在其中指定了我需要在回复中包含的 3 个字段:id、json 和说明。 由于我在 PostgreSQL 数据库中使用 JSONField,因此我的 json 字段是存储在数据库中的 json,我使用 JSONSerializerField 渲染它。

这是我的代码:

class JSONSerializerField(serializers.Field):
    """ Serializer for JSONField -- required to make field writable"""
    def to_internal_value(self, data):
        return data
    def to_representation(self, value):
        return value

class QuestionSerializer(serializers.ModelSerializer):
    content = JSONSerializerField(source='json')

    class Meta:
        model = Question
        fields = ('id', 'content', 'explanation')

所以,每次我使用 QuestionSerializer 时,我的回复都会是这样的:

{
    "id": 1,
    "content": {
        "question": "question",
        "answers": [
            {"answer": "answer"},
            {"answer": "answer"},
            {"answer": "answer"},
            {"answer": "answer"},
            {"answer": "answer"}
        ],
    }
    "explanation": "explanation"
}

但我需要删除“内容”字段才能得到如下响应:

{
    "id": 1,
    "question": "question",
    "answers": [
        {"answer": "answer"},
        {"answer": "answer"},
        {"answer": "answer"},
        {"answer": "answer"},
        {"answer": "answer"}
    ],
    "explanation": "explanation"
}

我该怎么办?

谢谢!

【问题讨论】:

    标签: json django django-rest-framework


    【解决方案1】:

    您可以创建content write_only 字段并使用它来存储数据。添加两个新的 read_only 字段并使用它们来获取如下数据:

    class QuestionSerializer(serializers.ModelSerializer):
        content = JSONSerializerField(source='json', write_only=True)
        question = serializers.SerializerMethodField()
        answers = serializers.SerializerMethodField()
    
        class Meta:
            model = Question
            fields = ('id', 'content','question', 'answers', 'explanation')
    
        def get_questions(self, obj):
            return obj.content['question']
    
        def get_answers(self, obj):
            return obj.content['answers']
    

    【讨论】:

    • 谢谢阿努什!我刚刚发布了正确的解决方案,但由于您的回答,我找到了它。
    【解决方案2】:

    我找到了正确的解决方案。我删除了“内容”字段和 JSONSerializerField 类,并开始使用 SerializerMethodField 处理我的 json。

    class QuestionSerializer(serializers.ModelSerializer):
        question = serializers.SerializerMethodField()
        answers = serializers.SerializerMethodField()
    
        class Meta:
            model = Question
            fields = ('id', 'question', 'answers', 'explanation')
    
        def get_question(self, obj):
            return obj.json['question']
    
        def get_answers(self, obj):
            return obj.json['answers']
    

    【讨论】:

      【解决方案3】:

      您应该更新您的字段,以便它直接返回/读取内容:

      class JSONSerializerField(serializers.Field):
          """ Serializer for JSONField -- required to make field writable"""
          def to_internal_value(self, data):
              return {'content': data}
          def to_representation(self, value):
              return value['content']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 2021-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-31
        相关资源
        最近更新 更多