I'm trying to create a new task in my database according to the todo_ID which I will put in the url. I can post a new task in the database entirely but I want the server to choose the todo_ID so if a person types /1 in the url it would create a new task with todo_ID = 1 and fill in the UserID and details depending on what the user puts in. Problem is I have no idea how to do it based on the todo_ID so can someone tell me how :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()