【问题标题】:python hug - Cannot use Nested in MarshmallowSchemapython 拥抱 - 不能在 MarshmallowSchema 中使用嵌套
【发布时间】:2017-06-11 08:51:23
【问题描述】:

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

我无法在 HUG 中使用 Marshmallow 的嵌套功能。这是一些代码。

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 的嵌套功能。如果是,怎么做?

【问题讨论】:

    标签: python marshmallow hug


    【解决方案1】:

    如果您尝试像这样定义端点并根据新签名访问数据会怎样?

    @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 可能会引起人们的兴趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多