[英]TypeError: defined function got an unexpected keyword argument 'many
I have a problem with my python app.我的 python 应用程序有问题。 I am following a tutorial posted on: https://auth0.com/blog/developing-restful-apis-with-python-and-flask/我正在关注发布的教程: https://auth0.com/blog/developing-restful-apis-with-python-and-flask/
I try to post data to the app by power-shell:我尝试通过 power-shell 将数据发布到应用程序:
$params = @{amount=80; description='test_doc'}
Invoke-WebRequest -Uri http://127.0.0.1:5000/incomes -Method POST -Body ($params|ConvertTo-Json) -ContentType "application/json"
When i run the PS script i get an error from my python app:当我运行 PS 脚本时,我的 python 应用程序出现错误:
TypeError: make_income() got an unexpected keyword argument 'many'
My code looks like this:我的代码如下所示:
from marshmallow import post_load
from .transaction import Transaction, TransactionSchema
from .transaction_type import TransactionType
class Income(Transaction):
def __init__(self, description, amount):
super(Income, self).__init__(description, amount, TransactionType.INCOME)
def __repr__(self):
return '<Income(name={self.description!r})>'.format(self=self)
class IncomeSchema(TransactionSchema):
@post_load
def make_income(self, data):
return Income(**data)
How am i getting the argument many
into my function?我如何将many
论点纳入我的 function? Is this a marshmallow problem?这是棉花糖的问题吗?
I have tried adding **
but i get the same error:我试过添加**
,但我得到了同样的错误:
def make_income(self, **data):
return Income(**data)
I have also tried我也试过
def make_income(self, data, **kwargs):
return Income(**data)
Here is my transaction.py file这是我的 transaction.py 文件
import datetime as dt
from marshmallow import Schema, fields
class Transaction():
def __init__(self, description, amount, type):
self.description = description
self.amount = amount
self.created_at = dt.datetime.now()
self.type = type
def __repr__(self):
return '<Transaction(name={self.description!r})>'.format(self=self)
class TransactionSchema(Schema):
description = fields.Str()
amount = fields.Number()
created_at = fields.Date()
type = fields.Str()
In marsmallow 3, decorated methods (pre/post_dump/load,...) must swallow unknown kwargs.在 marsmallow 3 中,装饰方法(pre/post_dump/load,...)必须吞下未知的 kwargs。
class IncomeSchema(TransactionSchema):
@post_load
def make_income(self, data, **kwargs):
return Income(**data)
(You may want to notify the blog author about this.) (您可能需要就此通知博客作者。)
In addition to accepted solution, please make the below changes
@app.route('/incomes')
def get_incomes():
schema = IncomeSchema(many=True)
incomes = schema.dump(
filter(lambda t: t.type == TransactionType.INCOME, transactions)
)
return jsonify(incomes.data) //change it to return jsonify(incomes)
@app.route('/incomes', methods=['POST'])
def add_income():
income = IncomeSchema().load(request.get_json())
transactions.append(income.data)//change it to transactions.append(income)
return "", 204
@app.route('/expenses')
def get_expenses():
schema = ExpenseSchema(many=True)
expenses = schema.dump(
filter(lambda t: t.type == TransactionType.EXPENSE, transactions)
)
return jsonify(expenses.data)// change it to return jsonify(expenses)
@app.route('/expenses', methods=['POST'])
def add_expense():
expense = ExpenseSchema().load(request.get_json())
transactions.append(expense.data) // change it to transactions.append(expense)
return "", 204
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.