【问题标题】:customize Django Rest Framework serializers output?自定义 Django Rest Framework 序列化程序输出?
【发布时间】:2016-11-28 19:24:10
【问题描述】:

我有一个这样的 Django 模型:

class Sections(models.Model):
    section_id = models.CharField(max_length=127, null=True, blank=True)
    title = models.CharField(max_length=255)
    description = models.TextField(null=True, blank=True)

class Risk(models.Model):
    title = models.CharField(max_length=256, null=False, blank=False)
    section = models.ForeignKey(Sections, related_name='risks')

class Actions(models.Model):
    title = models.CharField(max_length=256, null=False, blank=False)
    section = models.ForeignKey(Sections, related_name='actions')

还有这样的序列化器:

class RiskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Risk
        fields = ('id', 'title',)

class ActionsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Actions
        fields = ('id', 'title',)

class RiskActionPerSectionsSerializer(serializers.ModelSerializer):
   risks = RiskSerializer(many=True, read_only=True)
   actions = ActionsSerializer(many=True, read_only=True)

   class Meta:
        model = Sections
        fields = ('section_id', 'risks', 'actions')
        depth = 1

通过视图访问 RiskActionPerSectionSerializer 时,我得到以下输出:

[
{
    "actions": [
        {
            "id": 2, 
            "title": "Actions 2"
        }, 
        {
            "id": 1, 
            "title": "Actions 1"
        }
    ], 
    "risks": [
        {
            "id": 2, 
            "title": "Risk 2"
        }, 
        {
            "id": 1, 
            "title": "Risk 1"
        }
    ], 
    "section_id": "section 1"
}
.....
]

没关系,但我更喜欢那个:

{
"section 1": {
    "risks": [
        {
          "id": 2,
          "title": "Risk 2"
        }, 
        {
          "id": 1,
          "title": "Risk 1"
        }
    ],
    "actions": [
        {
          "id": 2,
          "title": "Actions 2"
        }, 
        {
          "id": 1,
          "title": "Actions 1"
        }
   ]
}
}

如何使用 Django Rest Framework 做到这一点?

【问题讨论】:

    标签: python json django django-rest-framework


    【解决方案1】:

    您可以覆盖序列化程序的to_representation() 方法:

    class RiskActionPerSectionsSerializer(serializers.ModelSerializer):
    
       class Meta:
           model = Sections
           fields = ('section_id', 'risks', 'actions')
           depth = 1
    
       def to_representation(self, instance):
           response_dict = dict()
           response_dict[instance.section_id] = {
               'actions': ActionsSerializer(instance.actions.all(), many=True).data,
               'risks': RiskSerializer(instance.risks.all(), many=True).data
           }
           return response_dict
    

    【讨论】:

    • 如何将输出结构更改为更像这样: [ "section1": { "actions": [], "risks": [] }, "section2": { "actions" : [], "风险": [] }, ]
    • @user659154 你最好问新问题,我现在想不通
    猜你喜欢
    • 2013-08-03
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2020-08-04
    相关资源
    最近更新 更多