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

时间:2018-05-17 02:36:01

标签: python postgresql flask flask-sqlalchemy

我是新的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()

和后期方法:

@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'

我做错了什么?

第一份印刷声明:

  

{'目标':[{'名称':' afasdfdsa'}],' name':' sdasdfdsaf&# 39 ;,   '描述':' df
  asfasdf',' startDate':' 05/31/2018',' endDate':' 05/31/2018',   ' allowTeamToVote&#39 ;:
  是的,' votingDeadline':' 05/31/2018'}

第二份印刷声明:

  

{' allowTeamToVote':是的,'名称':' asfdfsd',' description':   ' dsfasdfs'}

完全例外:

  

127.0.0.1 - - [16 / May / 2018 20:43:43]"选项/策略HTTP / 1.1" 200 - [2018-05-16 20:43:44,712]应用程序中的错误:/策略的例外情况   [POST] Traceback(最近一次调用最后一次):文件   " C:\用户\ jason.virtualenvs \后端-foekiio5 \ lib中\站点包\烧瓶\ app.py&#34 ;,   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中       return cors_after_request(app.make_response(f(* args,** kwargs)))文件   " C:\用户\ jason.virtualenvs \后端-foekiio5 \ lib中\站点包\烧瓶\ app.py&#34 ;,   第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-packages \ 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中       return self.view_functionsrule.endpoint File" C:\ projects \ Python Projects \ three-back \ backend \ src \ auth.py",line   101,装饰       返回f(* args,** kwargs)文件" C:\ projects \ Python Projects \ three-back \ backend \ src \ main.py",第41行,在add_strategy中       strategy =策略(** published_strategy.data,created_by =" HTTP post request")TypeError: init ()缺少3个必需的位置   争论:' startDate',' endDate'和' votingDeadline'   127.0.0.1 - - [16 / May / 2018 20:43:44]" POST /策略HTTP / 1.1" 500 -

1 个答案:

答案 0 :(得分:0)

  

.load(request.get_json())方法无法转换的内容   被称为所有三个日期时间值。

您的第二个print语句不属于该函数。所以你输错了。

  

我收到以下错误:

您正在将dict(键值对)传递给方法,但它期待位置参数。

将此行更改为:

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

Marshmallow有另一种方法可以达到相同的效果,如documentation here中所述。