我的python应用有问题。我正在关注发布在以下位置的教程:https://auth0.com/blog/developing-restful-apis-with-python-and-flask/
我尝试通过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"
当我运行PS脚本时,我的python应用程序出现错误:
TypeError: make_income() got an unexpected keyword argument 'many'
我的代码如下所示:
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)
我如何将参数many
引入函数?这是棉花糖的问题吗?
我尝试添加,**
但出现相同的错误:
def make_income(self, **data):
return Income(**data)
我也尝试过
def make_income(self, data, **kwargs):
return Income(**data)
这是我的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()
在marsmallow 3中,修饰的方法(pre / post_dump / load等)必须吞噬未知的kwarg。
class IncomeSchema(TransactionSchema):
@post_load
def make_income(self, data, **kwargs):
return Income(**data)
(您可能需要将此事通知博客作者。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句