python拥抱 - 不能在MarshmallowSchema中使用嵌套

时间:2017-06-11 08:51:24

标签: python marshmallow hug

使用HUG构建Rest API,VERSION 2.3.0(https://github.com/timothycrosley/hug

我不能在HUG中使用棉花糖的嵌套能力。这是一些代码。

import config
from tinydb import TinyDB, Query
import hug
import hashlib
import logging
import os
from marshmallow import Schema, fields, validates, ValidationError

class MultilanguageSchema(Schema):
    language = fields.Str()
    value = fields.Str()

class TitleSchema(Schema):
    titles = fields.Nested(MultilanguageSchema, many = True)

class DescriptionSchema(Schema):
    descriptions = fields.Nested(MultilanguageSchema, many = True)

@hug.post('/ads', requires=api_key_authentication, versions=1)
def post_ad(sites_id: hug.types.number,
        name: hug.types.text,
        age: hug.types.in_range(18, 99),
        telephone: hug.types.number,

        titles: hug.types.MarshmallowSchema(TitleSchema()),
        descriptions: hug.types.MarshmallowSchema(DescriptionSchema()),

        authed_user: hug.directives.user):

    pdb.set_trace()

当我尝试使用此示例数据执行POST请求时,我在嵌套字段上获得了空数据。

sites_id: 1
name: Test Name
age: 34
telephone: 999999999
titles: {"language":"en","value":"Some Title"}
descriptions: {"language":"en","value":"Some Description"} 

使用pdb:

titles: {}
descriptions: {}

任何人都可以在HUG中使用Marshmallow的嵌套技能。如果是的话,该怎么做?

1 个答案:

答案 0 :(得分:0)

如果您尝试以此方式定义端点并基于新签名访问数据会发生什么?

@hug.post('/ads')
def post_ad(*args, **kwargs):
    print('Args: %s' % args)
    print('Kwargs: %s' % kwargs)
    try:
        sites_id = kwargs.get('sites_id', None)
    except Exception as e:
        print(e)

这里提出的issue可能很有趣。