简体   繁体   English

使用 DRF 序列化器验证包含动态键的嵌套字典

[英]Validating nested dictionaries containing dynamic keys using DRF Serializers

I am working on writing serializers for an existing API for the purpose of auto-generating OpenAPI documentation for the project.我正在为现有 API 编写序列化程序,以便为项目自动生成 OpenAPI 文档。 One of the issues I'm running into is defining serializers to validate the returned data from some of the views.我遇到的问题之一是定义序列化程序来验证从某些视图返回的数据。

Here is the structure of the data:这是数据的结构:

{
    "airmass_data": {
        "site-A": {
            "times": ["2021-09-09T09:54","2021-09-09T10:04"],
            "airmasses": [2.900041850251779, 2.687634707193725]
        },
        "site-B": {
            "times": ["2021-09-09T09:54","2021-09-09T10:04"],
            "airmasses": [2.900041850251779, 2.687634707193725]
        },
                ...
    },
    "airmass_limit": 3.19
}

There can be an arbitrary number of "site-X" keys, and they are dynamically generated - which is definitely part of my issue.可以有任意数量的“site-X”键,它们是动态生成的——这绝对是我的问题的一部分。

Here is how I've set up my serializers, which I think mostly matches my dictionary structure:以下是我设置序列化程序的方式,我认为这与我的字典结构最匹配:

class SiteAirmassDatumSerializer(serializers.Serializer):
    times = serializers.ListField(child=serializers.CharField())
    airmasses = serializers.ListField(child=serializers.FloatField())


class SiteAirmassSerializer(serializers.Serializer):
    site = SiteAirmassDatumSerializer(source='*')


class AirmassSerializer(serializers.Serializer):
    airmass_data = SiteAirmassSerializer(source='*')
    airmass_limit = serializers.FloatField()

However, when passing my dictionary into the the serializer and attempting to validate it, the serializer.errors attribute has:但是,当将我的字典传递给序列化程序并尝试对其进行验证时, serializer.errors 属性具有:

{
  "airmass_data": {
    "site": [
      "This field is required."
    ]
  }
}

Is there a good way to write a set of serializers such that it deals with dynamically generated keys?有没有一种好方法可以编写一组序列化程序来处理动态生成的密钥? I am mostly trying to write this to validate the general structure of the returned dictionary - not necessarily the keys within it.我主要是想写这个来验证返回字典的一般结构——不一定是其中的键。 The reason I am interested in using Serializers is to utilize DRF's OpenAPI generation features.我对使用序列化程序感兴趣的原因是利用 DRF 的 OpenAPI 生成功能。

EDIT:编辑:

Have also tried using a DictField in the serializers, like so:还尝试在序列化程序中使用DictField ,如下所示:

class SiteAirmassDatumSerializer(serializers.Serializer):
    times = serializers.ListField(child=serializers.CharField())
    airmasses = serializers.ListField(child=serializers.FloatField())


class SiteAirmassSerializer(serializers.Serializer):
    site = DictField(child=SiteAirmassDatumSerializer(), source='*')


class AirmassSerializer(serializers.Serializer):
    airmass_data = DictField(child=SiteAirmassSerializer(), source='*')
    airmass_limit = serializers.FloatField()

When attempting to validate the aforementioned structure, I get the following error:尝试验证上述结构时,出现以下错误:

{
  "airmass_data": {
    "site-A": {
      "site": [
        "This field is required."
      ]
    }
  }
}

I suggest changing your data structure and use a list instead of dict:我建议更改您的数据结构并使用列表而不是字典:

{
    "airmass_data": [
         {
            "site_name": "site-A"
            "times": ["2021-09-09T09:54","2021-09-09T10:04"],
            "airmasses": [2.900041850251779, 2.687634707193725]
        },
        {
            "site_name": "site-B"
            "times": ["2021-09-09T09:54","2021-09-09T10:04"],
            "airmasses": [2.900041850251779, 2.687634707193725]
        },
                ...
    ],
    "airmass_limit": 3.19
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM