使用带有POST的id在数据库中创建新任务

时间:2016-11-20 02:25:39

标签: python rest post flask

我试图根据我将放在网址中的todo_ID在我的数据库中创建一个新任务。我可以完全在数据库中发布一个新任务,但我希望服务器选择todo_ID,这样如果一个人在url中键入/ 1,它将创建一个todo_ID = 1的新任务,并根据内容填写UserID和详细信息用户输入。问题是我不知道如何根据todo_ID这样做,所以有人可以告诉我如何:D

from flask import Flask, jsonify,json, request, abort
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_pyfile('Config.py')
db = SQLAlchemy(app)


class User(db.Model, JsonModel): #Class which is a model for the User table in the database
    User_ID = db.Column(db.Integer, primary_key = True)
    FirstName = db.Column(db.String(20))
    LastName = db.Column(db.String(20))

    def __init__(self,User_ID,FirstName, LastName):
        self.User_ID = User_ID
        self.FirstName = FirstName
        self.LastName = LastName

class Todo(db.Model, JsonModel):    #Class which is a model for the Todo table in the database
    todo_ID = db.Column(db.Integer, primary_key = True)
    UserID = db.Column(db.Integer, db.ForeignKey("user.User_ID"))
    details = db.Column(db.String(30))

    def __init__(self, UserID, details):
        self.UserID = UserID
        self.details = details


  @app.route('/<int:todo_ID>', methods = ['POST'])  #Uses POST method with same URL as GET method to add new information to Todo table.
def create_dev(todo_ID):
    id = Todo.query.create(todo_ID)
    dev = request.json["UserID"],request.json["details"]
    db.session.add(id,dev)
    db.session.commit()
    return "New task has been created.", 201


@app.before_first_request #Creates everything before the first request.
def startup():
    db.create_all()

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

1 个答案:

答案 0 :(得分:0)

尝试

dev.UserID = request.POST["UserID"]

要使request.json正常工作,请将内容类型设置为application / json。来自Flask documentation

  

如果mimetype是application / json,则它将包含已解析的JSON数据。否则这将是None。

有关详情:https://stackoverflow.com/a/20001283/3930114

编辑1:基于评论中讨论的解决方案

@app.route('/<int:todo_ID>', methods = ['POST'])  #Uses POST method    with same URL as GET method to add new information to Todo table.
# Setting todo_ID default to None
def create_dev(todo_ID=None):
    # This if will allow you to not pass id via url and handle in backend. Frankly, first integer field will automatically become autoincrement field in Flask, so I am not even sure why you want to send todo_ID via url but I dont have enough information about your app :)
    if todo_ID is None:
        # write logic to create a todo_ID
        pass
    # Instance of model task
    dev.UserID = request.json["UserID"]
    dev.details = request.json["details"]
    t = Task(todo_ID, dev.UserID, dev.details)
    db.session.add(t)
    db.session.commit()
    return "New task has been created with todo_ID: " + todo_ID, 201

编辑2:基于评论中的其他信息的解决方案

@app.route('/', methods = ['POST'])  #Uses POST method    with same URL as GET method to add new information to Todo table.
# Setting todo_ID default to None
def create_dev():
    # Instance of model task
    t = Todo(UserID=request.json["UserID"], details=request.json["details"])
    db.session.add(t)
    db.session.commit()
    return "New task has been created with todo_ID: " + todo_ID, 201

PS:我还没有测试过这段代码。

参考文献: