简体   繁体   English

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

[英]Missing required positional argument (datetime)

I am new Flask and Postgres... 我是新的Flask和Postgres ...

I am following this tutorial: https://auth0.com/blog/using-python-flask-and-angular-to-build-modern-apps-part-1/ 我正在关注本教程: https : //auth0.com/blog/using-python-flask-and-angular-to-build-modern-apps-part-1/

I have created a Entity like this: 我创建了一个这样的实体:

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()

and a Post Method: 和一个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

What doesn't getting converted when .load(request.get_json()) method is called is all three datetime values. 调用.load(request.get_json())方法时未转换的是所有三个datetime值。

I get the following error: 我收到以下错误:

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

What am I doing wrong? 我究竟做错了什么?


1st print statement: 第一份印刷声明:

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

2nd print statement: 第二份印刷声明:

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

Full Exception: 完全例外:

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-p 127.0.0.1--[16 / May / 2018 20:43:43]“ OPTIONS / strategys HTTP / 1.1” 200-[2018-05-16 20:43:44,712]应用程序中出现错误:/ strategies异常[POST]追溯(最近一次通话最近):文件“ c:\\ users \\ jason.virtualenvs \\ backend-foekiio5 \\ lib \\ site-packages \\ flask \\ app.py”,行1982,在wsgi_app response = self.full_dispatch_request()文件中c:\\ users \\ jason.virtualenvs \\ backend-foekiio5 \\ lib \\ site-packages \\ flask \\ app.py“,行1614,在full_dispatch_request中rv = self.handle_user_exception(e)文件” c:\\ users \\ jason.virtualenvs \\ backend-foekiio5 \\ lib \\ site-packages \\ flask_cors \\ extension.py“,第161行,在wrapped_function中,返回cors_after_request(app.make_response(f(* args,** kwargs)))文件” c:\\ users \\ jason.virtualenvs \\ backend-foekiio5 \\ lib \\ site-packages \\ flask \\ app.py“,第1517行,在handle_user_exception reraise中(exc_type,exc_value,tb)文件“ c:\\ users \\ jason.virtualenvs \\ backend-foekiio5 \\ lib \\ site- “ packages \\ flask_compat.py”,第33行,重新提高价值文件“ c:\\ users \\ jason.virtualenvs \\ backend-foekiio5 \\ lib \\ site-p ackages\\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 - ackages \\ flask \\ app.py”,行1612,在full_dispatch_request中rv = self.dispatch_request()文件“ c:\\ users \\ jason.virtualenvs \\ backend-foekiio5 \\ lib \\ site-packages \\ flask \\ app.py”,行1598,在dispatch_request中返回self.view_functionsrule.endpoint文件“ C:\\ projects \\ Python Projects \\ three-back \\ backend \\ src \\ auth.py”,行101,在修饰后的return f(* args,** kwargs)File“ C:\\ projects \\ Python Projects \\ three-back \\ backend \\ src \\ main.py“,第41行,位于add_strategy strategy = Strategy(** posted_strategy.data,created_by =” HTTP post request“)TypeError: init ()missing 3个必需的位置参数:“ startDate”,“ endDate”和“ votingDeadline” 127.0.0.1--[16 / May / 2018 20:43:44]“ POST / strategys HTTP / 1.1” 500-

What doesn't getting converted when .load(request.get_json()) method is called is all three datetime values. 调用.load(request.get_json())方法时未转换的是所有三个datetime值。

Your second print statement is outside of the function. 您的第二个print语句不在函数之外。 So you are getting the wrong output. 所以您得到错误的输出。

I get the following error: 我收到以下错误:

You are passing a dict(key-value pairs) to the method but it is expecting positional arguments. 您正在将dict(键值对)传递给方法,但它需要位置参数。

Change this line to this: 将此行更改为此:

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

In all, something like this: 总而言之,是这样的:

@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")

Marshmallow has another way of achieving the same result, as noted in the documentation here . 如此处的文档所述,棉花糖还有另一种获得相同结果的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM