Skip to content

Commit

Permalink
Update docs about unknown.EXCLUDE for arguments schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Feb 29, 2020
1 parent 8460b6b commit ce59659
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/arguments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,64 @@ view function.
def post(pet_data, query_args):
return Pet.create(pet_data, **query_args)
Multiple Arguments Schemas
--------------------------

To define arguments from multiple locations, calls to ``arguments`` decorator
can be stacked:

.. code-block:: python
:emphasize-lines: 4,5
@blp.route('/')
class Pets(MethodView):
@blp.arguments(PetSchema)
@blp.arguments(QueryArgsSchema, location="query")
@blp.response(PetSchema, code=201)
def post(self, pet_data, query_args):
pet = Pet.create(**pet_data)
# Use query args
...
return pet
It is possible to define multiple arguments for the same location. However,
with marshmallow 3, schemas raise by default on unknown fields, so they should
be tweaked to define ``unknown`` meta attribute as ``EXCLUDE``.

.. code-block:: python
:emphasize-lines: 5,12
# With marshmallow 3, define unknown=EXCLUDE
class QueryArgsSchema1(ma.Schema):
class Meta:
ordered = True
unknown = ma.EXCLUDE
arg1 = ma.fields.String()
arg2 = ma.fields.Integer()
class QueryArgsSchema2(ma.Schema):
class Meta:
ordered = True
unknown = ma.EXCLUDE
arg3 = ma.fields.String()
arg4 = ma.fields.Integer()
@blp.route('/')
class Pets(MethodView):
@blp.arguments(QueryArgsSchema1, location="query")
@blp.arguments(QueryArgsSchema2, location="query")
@blp.response(PetSchema, code=201)
def get(self, query_args_1, query_args_2):
query = {}
query.update(query_args_1)
query.update(query_args_2)
return Pet.get(**query)
This also applies when using both query arguments and ``pagination`` decorator,
as the pagination feature uses query arguments for pagination parameters.

Content Type
------------

Expand Down

0 comments on commit ce59659

Please sign in to comment.