使用参数时,flask-restplus会产生“为关键字参数“ strict”获得多个值”

时间:2019-02-09 02:54:05

标签: python python-3.x flask typeerror flask-restplus

我正在使用使用flask-restplus打补丁(flask-restplus +烧瓶棉花糖支持)的REST API。以https://github.com/frol/flask-restplus-server-example为例,在我尝试向add_user函数添加参数之前,一切都进行得很好。我的代码在下面。

users.py

from flask_restplus_patched import Resource, Namespace
from flask_restplus_patched._http import HTTPStatus
from database.models import db, User
from . import schemas, parameters

api = Namespace('user', description='User related operations') 

@api.route('/')
class Users(Resource):
    @api.doc('list_users')
    @api.response(schemas.BaseUserSchema(many=True))
    def get(self):
        return User.query.all()

    @api.parameters(parameters.AddUserParameters())
    @api.response(schemas.DetailedUserSchema())
    @api.doc('add_user')
    def post(self, args):
        new_user = User(**args)
        db.session.add(new_user)
        db.session.commit()
        return new_user

parameters.py

from . import schemas
from database.models import User
from flask_restplus_patched import PostFormParameters
from flask_marshmallow import base_fields

class AddUserParameters(PostFormParameters, schemas.BaseUserSchema):
    name = base_fields.String(required=True)
    email = base_fields.String(required=True)

schemas.py

from flask_restplus_patched import Schema, ModelSchema
from database.models import User

class BaseUserSchema(ModelSchema):
    class Meta:
        model = User
        fields = (
            User.id.key,
            User.name.key,
            User.email.key,
            User.risk.key,
        )

class DetailedUserSchema(BaseUserSchema):
    class Meta(BaseUserSchema.Meta):
        fields = BaseUserSchema.Meta.fields + (
            User.notes.key,
            User.applications.key,
            User.scopes.key,
        )

当我尝试启动我的应用程序时,我收到呼叫跟踪:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from api import api
  File "/Users/user/code/gosa_v2/src/api/__init__.py", line 2, in <module>
    from .users.resources import api as users_api
  File "/Users/user/code/gosa_v2/src/api/users/resources.py", line 12, in <module>
    class Users(Resource):
  File "/Users/user/code/gosa_v2/src/api/users/resources.py", line 18, in Users
    @api.parameters(parameters.AddUserParameters())
  File "/Users/user/Library/Python/3.6/lib/python/site-packages/flask_restplus_patched/parameters.py", line 44, in __init__
    super(PostFormParameters, self).__init__(*args, **kwargs)
  File "/Users/user/Library/Python/3.6/lib/python/site-packages/flask_restplus_patched/parameters.py", line 20, in __init__
    super(Parameters, self).__init__(strict=True, **kwargs)
  File "/Users/user/Library/Python/3.6/lib/python/site-packages/flask_restplus_patched/model.py", line 31, in __init__
    super(ModelSchema, self).__init__(strict=True, **kwargs)
TypeError: __init__() got multiple values for keyword argument 'strict'

据我所知,strict仅设置为True。如果我从users.py中删除@api.parameters(parameters.AddUserParameters()),则该应用程序将运行。

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案