【问题标题】:is it possible to dynamically change field name within django-rest-framework serializer?是否可以在 django-rest-framework 序列化程序中动态更改字段名称?
【发布时间】:2017-08-26 16:34:11
【问题描述】:

我有 ProductProductCategory 模型。

假设我有 ProductCategory TV,它有 Sony, Samsung 作为它的产品。我还有MobilePhone 类别,其中AppleNokia 作为其产品。 使用 DRF,我想使用序列化程序获取 JSON 输出,类似于以下内容:

{
    'TV': 
        [
            'Sony': 
                {
                    'price': '$100',
                    'country': 'Japan',
                },
            'Samsung': 
                {
                    'price': '$110',
                    'country': 'Korea',
                }
        ]

    'mobile_phone':
        [
            'Apple': 
                {
                    'price': '$300',
                    'country': 'USA',
                },
            'Nokia': 
                {
                    'price': '$210',
                    'country': 'Finland',
                }
        ]
}

这里的问题是序列化器中的字段names('TV', 'mobile_phone')必须是动态的。

我知道我可以得到以下 JSON 类型

{ 
    [
            {   
                'product_category': 'TV',
                'manufacturer: 'Sony',
                'price': '$100',
                'country': 'Japan',
            },
            {
                'product_category': 'TV',
                'manufacturer: 'Samgsung',
                'price': '$110',
                'country': 'Korea',
            }
    ]

    [
            {
                'product_category': 'mobile_phone',
                'manufacturer: 'Samgsung',
                'price': '$300',
                'country': 'USA',
            },
            {
                'product_category': 'mobile_phone',
                'manufacturer: 'Apple',
                'price': '$210',
                'country': 'Finland',
            }
    ]
}

class CategorySerializer(serializers.Serializer):
    product_category = serializer.CharField()
    manufacturer = serializer.CharField()
    price = serializer.CharField()
    country = serializer.CharField()

但是动态变化的字段名称很难实现。有什么办法可以做到这一点吗?

【问题讨论】:

    标签: json django serialization django-rest-framework


    【解决方案1】:

    您可以通过覆盖您的 Serializer 和默认 ListSerializer 的 to_representation 来支持这种自定义格式

    1. 首先,您覆盖您的序列化程序的to_representation

      class CategorySerializer(serializers.Serializer):
          # your fields here
      
          def to_representation(self, obj):
            return {
                obj.manufacturer: {
                   'price': obj.price,
                   'country': obj.country
                }
            }
      

      所以你的序列化类别是这种形式:

      {
          'Sony': {
            'price': '$100',
            'country': 'Japan'
          }
      }
      
    2. 然后要在列表前面添加product_category,您可以使用自定义ListSerializer和自定义to_representation

      class CategoryListSerializer(serializers.ListSerializer):
      
          def to_representation(self, data):
              # Group your data entries by category here
              ...
              return {
                  'TV': tv_data
                  'mobile_phone': mobile_data
              }
      
      class CategorySerializer(serializers.Serializer):
          ...
          class Meta:
            list_serializer_class = CategoryListSerializer
      

    【讨论】:

    • 哇,这是一个绝妙的答案;易于理解,同时非常全面。非常感谢您花时间解释这么详细!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2013-07-07
    • 2017-09-01
    • 2016-09-23
    相关资源
    最近更新 更多