Flask Restx模型嵌套通配符dict

问题描述

我想知道我是否只是一个正在努力解决此类问题的人。

让我们以dict为例:

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

请不要将 ram1 / 2 是无法预先知道的动态键

问题,我的api.model应该如何显示我有

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),})

它正在运行,但是它不会验证“发行版”以下的任何内容,换句话说,它像通配符一样工作,任何可以接受的内容。 有没有办法以具有通配符动态键的方式嵌套字典?

解决方法

首先,var config = { url: 'ldap://domain.com',baseDN: 'dc=domain,dc=com ' } var groupName = 'something'; var ad = new ActiveDirectory(config); ad.getUsersForGroup(groupName,function(err,users) { if (err) { console.log('ERROR: ' +JSON.stringify(err)); return; } if (! users) console.log('Group: ' + groupName + ' not found.'); else { console.log(JSON.stringify(users)); } });``` I am new to LDAP. So really appreciate any kind of help 类型的字段接受dict值的定义,而不接受键的定义,即Wildcard验证dict值只能是字符串类型(在您的情况下您需要提供分布的定义。)

第二个错误是您将fields.Wildcard(fields.String())字段定义为distribution对象,而不是使用Nested

以下代码应可用于验证目的:

Wilcard

不幸的是,它不适用于封送处理。 下一个代码应适用于封送处理,但不适用于验证输入有效负载:


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))
))