简体   繁体   English

Flask restx model 嵌套通配符字典

[英]Flask restx model nested wildcard dict

I was wondering if I'm only one struggling with such problem.我想知道我是否只是一个在这个问题上苦苦挣扎的人。

Lets take dict for example:让我们以 dict 为例:

data = {'totalSize': 3000, 'freq': 2400,
        'distribution':
            {'ram1': {'size': 200, 'status': 'OK'},
             'ram2': {'size': 100, 'status': 'OK'}
             }
        }

Please not that ram1/2 is dynamic keys that can not be known in advance请注意ram1/2是无法提前知道的动态键

Question, how should my api.model look like?问题,我的 api.model 应该是什么样子? I have:我有:

wild = {"*": fields.Wildcard(fields.String())}
hw_memory = api.model('Memory', {
    'totalSize': fields.Integer(description='total memory size in MB',
                                example=1024),
    'freq': fields.Integer(description='Speed of ram in mhz', example=800),
    'distribution': fields.Nested(wild),
})

It is working, however it does not validate anything below "distribution", in other words, works like wildcard, anything there will be accepted.它正在工作,但是它不会验证“分发”以下的任何内容,换句话说,就像通配符一样工作,任何东西都将被接受。 Is there a way to nest dicts in such way having wildcard dynamic key?有没有办法以具有通配符动态键的方式嵌套字典?

First of all, Wildcard type of field accepts the definition of the dict values, not the definition of the keys, ie fields.Wildcard(fields.String()) validates that dict values can be only of string type (in your case you need to provide definition of distribution).首先, Wildcard类型的字段接受字典值的定义,而不是键的定义,即fields.Wildcard(fields.String())验证字典值只能是字符串类型(在你的情况下你需要提供分布的定义)。

The second mistake is that you are defining distribution field as Nested object instead of using Wilcard .第二个错误是您将distribution字段定义为Nested object 而不是使用Wilcard

The following code should work for validation purpose:以下代码应用于验证目的:


DISTRIBUTION_MODEL = NAMESPACE.model("Distribution", dict(
    size=fields.Integer(),
    status=fields.String(),
))

MEMORY_MODEL = NAMESPACE.model("Memory", dict(
    totalSize=fields.Integer(description='total memory size in MB',
                             example=1024),
    freq=fields.Integer(description='Speed of ram in mhz', example=800),
    distribution=fields.Wildcard(fields.Nested(DISTRIBUTION_MODEL))
))

Unfortunately, it doesn't work for marshaling.不幸的是,它不适用于编组。 The next code should work for marshaling, but doesn't for validation input payload:下一个代码应该适用于封送处理,但不适用于验证输入有效负载:


OUTPUT_MEMORY_MODEL = NAMESPACE.model("OutputMemory", dict(
    totalSize=fields.Integer(description='total memory size in MB',
                             example=1024),
    freq=fields.Integer(description='Speed of ram in mhz', example=800),
    distribution=flask_utils.fields.Nested(
        NAMESPACE.model(
            "distributions", {
                "*": fields.Wildcard(
                    # DISTRIBUTION_MODEL is taken from previous snippet
                    fields.Nested(DISTRIBUTION_MODEL)
                )
            }
        )
    )
))

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

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