简体   繁体   English

__init __()接受1个位置参数,但给出了3个

[英]__init__() takes 1 positional argument but 3 were given

I am trying to send a post request to the created table but keep getting the error init () takes 1 positional argument but 3 were given. 我正在尝试向创建的表发送发布请求,但不断收到错误init ()接受1个位置参数,但给出了3个。

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import os

app = Flask(__name__)
file_path = os.path.abspath(os.getcwd())+"\database.db"

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+file_path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

class post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    status = db.Column(db.Boolean(50))
    title = db.Column(db.String(200))
    body = db.Column(db.String(max))
    createdtime = db.Column(db.DateTime())

def __init__(self, status, title, body, createdtime):
    self.status = status
    self.title= title
    self.body = body
    self.createdtime = createdtime


@app.route('/api/posts', methods=['GET'])
def get_all_posts():
    return ''

@app.route('/api/posts/<id>', methods=['GET'])
def get_one_post():
    return ''

@app.route('/api/new-post', methods=['POST'])
def put_post():
    title= request.get_json('title')
    body= request.get_json('body')

    new_post= post(title, body)
    db.session.add(new_post)
    db.session.commit()
    return jsonify({'message': 'Post created Successfully'})


@app.route('/api/delete-post', methods=['DELETE'])
def delete_post():
    return ''

if __name__ == '__main__':
    app.run(debug=True)

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

plus my request looks something like this 再加上我的要求看起来像这样

{
 "title" : "Start before you are ready",
 "body": "This post will be a little different from what I'm used to 
 write. I tend to keep my articles in the technical realm of programming. 
 This time, I'll have to talk about a subject that transformed my mindset 
 lately. It will appear a little like a rant, and it very well may be :D"
}

First the __init__ method is not the the same indentation level as the post class. 首先, __init__方法与post类的缩进级别不同。 Secondly you don't need to override the default constructor of db.Model you can use the Post model. 其次,您不需要覆盖db.Model的默认构造函数,可以使用Post模型。

For example: post = Post(title='xx') like this you can pass as many argument to it like this when you instantiate the Post class as long as the attribute you are using is in the class. 例如: post = Post(title='xx') ,只要实例中使用的属性在类中,就可以在实例化Post类时向其传递尽可能多的参数。

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

相关问题 __init__() 接受 1 个位置参数,但给出了 2 个 - __init__() takes 1 positional argument but 2 were given __init __()接受1个位置参数,但给出了2个 - __init__() takes 1 positional argument but 2 were given _init__() 接受 1 个位置参数,但给出了 2 个 - _init__() takes 1 positional argument but 2 were given Python 继承 - 类型错误:__init__() 采用 1 个位置参数,但给出了 4 个 - Python Inheritance - TypeError: __init__() takes 1 positional argument but 4 were given TypeError:__ init __()采用1个位置参数,但给出了2个 - TypeError: __init__() takes 1 positional argument but 2 were given 单元测试+硒-__init __()接受1个位置参数,但给出了2个 - Unittest + Selenium - __init__() takes 1 positional argument but 2 were given TypeError:__init __()接受1个位置参数,但给出了4个 - TypeError: __init__() takes 1 positional argument but 4 were given 向导 django 2.1:__init__() 需要 1 个位置参数,但给出了 2 个 - Wizard django 2.1 : __init__() takes 1 positional argument but 2 were given Python __init__() 需要 1 个位置参数,但给出了 3 个 - Python __init__() takes 1 positional argument but 3 were given 解决错误类型错误:__init__() 需要 1 个位置参数,但给出了 2 个 - Solving the error TypeError: __init__() takes 1 positional argument but 2 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM