是否可以在django

编程入门 行业动态 更新时间:2024-10-26 18:24:13
本文介绍了是否可以在django-rest-framework序列化程序中动态更改字段名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有Product和ProductCategory模型.

假设我有ProductCategory TV,其中 Sony,Samsung作为其产品.我也有MobilePhone类别,其产品为Apple和Nokia. 使用DRF,我想使用序列化器获取JSON输出,如下所示:

Let's say I have ProductCategory TV, which has Sony, Samsung as its products. I also have MobilePhone category with Apple and Nokia as its products. Using DRF, I would like to get JSON output using serializers, which is similar to the below:

{ 'TV': [ 'Sony': { 'price': '$100', 'country': 'Japan', }, 'Samsung': { 'price': '$110', 'country': 'Korea', } ] 'mobile_phone': [ 'Apple': { 'price': '$300', 'country': 'USA', }, 'Nokia': { 'price': '$210', 'country': 'Finland', } ] }

这里的问题是序列化程序中的字段名称('TV', 'mobile_phone')必须是动态的.

The problem here is that the field names('TV', 'mobile_phone') in serializer have to be dynamic.

我知道我可以获取以下JSON类型

I know I can get the following JSON type

{ [ { '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()

但是,动态更改字段名称很难实现.有什么办法可以做到吗?

But the dynamically-changing-field-names is difficult to achieve. Is there any way I can do this?

推荐答案

您可以通过覆盖序列化程序和默认ListSerializer的to_representation :

You can support this kind of custom format by overriding to_representation of your Serializer and of the default ListSerializer:

  • 首先,您覆盖序列化程序的to_representation :

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

    这样您的序列化类别就是这种形式:

    So that your serialized categories are of this form:

    { 'Sony': { 'price': '$100', 'country': 'Japan' } }

  • 然后将product_category放在列表的前面,您可以使用自定义ListSerializer 和自定义to_representation:

  • Then to prepend the product_category in front of your list, you can use a custom ListSerializer with a custom 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

  • 更多推荐

    是否可以在django

    本文发布于:2023-10-10 01:17:50,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1477291.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:django

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!