gpt4 book ai didi

python - 缺少必需的位置参数(日期时间)

转载 作者:行者123 更新时间:2023-11-29 13:44:39 26 4
gpt4 key购买 nike

我是 Flask 和 Postgres 新手...

我正在学习本教程: https://auth0.com/blog/using-python-flask-and-angular-to-build-modern-apps-part-1/

我创建了一个这样的实体:

from marshmallow import Schema, fields
from sqlalchemy import Column, String, Boolean, Date

from .entity import Entity, Base


class Strategy(Entity, Base):
__tablename__ = 'strategies'

name = Column(String)
description = Column(String)
startDate = Column(Date)
endDate = Column(Date)
allowTeamToVote = Column(Boolean)
votingDeadline = Column(Date)
isActive = Column(Boolean)

def __init__(self, name, description, startDate, endDate, allowTeamToVote, votingDeadline, created_by):
Entity.__init__(self, created_by)
self.name = name
self.description = description
self.startDate = startDate
self.endDate = endDate
self.allowTeamToVote = allowTeamToVote
self.votingDeadline = votingDeadline
self.isActive = True

class StrategySchema(Schema):
id = fields.Number()
name = fields.Str()
description = fields.Str()
startDate = fields.DateTime()
endDate = fields.DateTime()
allowTeamToVote = fields.Boolean()
votingDeadline = fields.DateTime()
isActive = fields.Boolean()
created_at = fields.DateTime()
updated_at = fields.DateTime()
last_updated_by = fields.Str()

和一个 Post 方法:

@app.route('/strategies', methods=['POST'])
@requires_auth
def add_strategy():
# mount Strategy object
print(request.get_json())
posted_strategy = StrategySchema(only=('name', 'description', 'startDate', 'endDate', 'allowTeamToVote', 'votingDeadline', 'isActive'))\
.load(request.get_json())
print(posted_strategy.data)


strategy = Strategy(**posted_strategy.data, created_by="HTTP post request")

# persist Strategy
session = Session()
session.add(strategy)
session.commit()

# return created Strategy
new_strategy = StrategySchema().dump(Strategy).data
session.close()
return jsonify(new_strategy), 201

当调用 .load(request.get_json()) 方法时没有得到转换的是所有三个日期时间值。

我收到以下错误:

 __init__() missing 3 required positional arguments: 'startDate', 'endDate', and 'votingDeadline'

我做错了什么?


第一个打印语句:

{'objectives': [{'name': 'afasdfdsa'}], 'name': 'sdasdfdsaf', 'description': 'df
asfasdf', 'startDate': '05/31/2018', 'endDate': '05/31/2018', 'allowTeamToVote':
True, 'votingDeadline': '05/31/2018'}

第二个打印语句:

{'allowTeamToVote': True, 'name': 'asfdfsd', 'description': 'dsfasdfs'}

完整异常:

127.0.0.1 - - [16/May/2018 20:43:43] "OPTIONS /strategies HTTP/1.1" 200 - [2018-05-16 20:43:44,712] ERROR in app: Exception on /strategies [POST] Traceback (most recent call last): File "c:\users\jason.virtualenvs\backend-foekiio5\lib\site-packages\flask\app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "c:\users\jason.virtualenvs\backend-foekiio5\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "c:\users\jason.virtualenvs\backend-foekiio5\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "c:\users\jason.virtualenvs\backend-foekiio5\lib\site-packages\flask\app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "c:\users\jason.virtualenvs\backend-foekiio5\lib\site-packages\flask_compat.py", line 33, in reraise raise value File "c:\users\jason.virtualenvs\backend-foekiio5\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "c:\users\jason.virtualenvs\backend-foekiio5\lib\site-packages\flask\app.py", line 1598, in dispatch_request return self.view_functionsrule.endpoint File "C:\projects\Python Projects\three-back\backend\src\auth.py", line 101, in decorated return f(*args, **kwargs) File "C:\projects\Python Projects\three-back\backend\src\main.py", line 41, in add_strategy strategy = Strategy(**posted_strategy.data, created_by="HTTP post request") TypeError: init() missing 3 required positional arguments: 'startDate', 'endDate', and 'votingDeadline' 127.0.0.1 - - [16/May/2018 20:43:44] "POST /strategies HTTP/1.1" 500 -

最佳答案

What doesn't getting converted when .load(request.get_json()) method is called is all three datetime values.

您的第二个 print 语句在函数之外。所以你得到了错误的输出。

I get the following error:

您正在将字典(键值对)传递给该方法,但它需要位置参数。

将此行更改为:

`strategy = Strategy(*posted_strategy.data.values(), created_by="HTTP post request")`

总的来说,是这样的:

@app.route('/strategies', methods=['POST'])
@requires_auth
def add_strategy():
# mount Strategy object
print(request.get_json())
posted_strategy = StrategySchema(only=('name', 'description', 'startDate', 'endDate', 'allowTeamToVote', 'votingDeadline', 'isActive'))\
.load(request.get_json())
print(posted_strategy.data)


strategy = Strategy(*posted_strategy.data.values(), created_by="HTTP post request")

documentation here 中所述,Marshmallow 有另一种实现相同结果的方法。 .

关于python - 缺少必需的位置参数(日期时间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50382554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com