我刚刚接触Python/Flask并正在研究一对多关系。我试过一些解决方案,但都没有生效。我的问题在于“/add_about”路由中,我想要获取创建此帖子的用户ID,并且能够在数据库中看到这个反映。
下面是我的代码:
from flask import Blueprint, jsonify, request
from flask_jwt_extended import jwt_required, current_user, get_current_user, get_jwt_identity
from app import db
from models.about import About, AboutSchema
from models.users import User
about = Blueprint('about', __name__)
about_schema = AboutSchema()
abouts_schema = AboutSchema(many=True)
@about.route('/')
def hello():
return "Oh LA LA LA LA !!!!!!"
# Get all abouts:
@about.route('/all', methods=['GET'])
def abouts():
abouts_list = About.query.all()
result = abouts_schema.dump(abouts_list)
return jsonify(result)
@about.route('/add_about', methods=['POST'])
@jwt_required
def add_about():
description = request.form['description']
user_id = get_jwt_identity() #I tried using "current_user" an "get_current_user"
new_about = About(description=description, user=user_id)
db.session.add(new_about)
db.session.commit()
return jsonify(message="You added a bio"), 201
这里是数据库模型:
from sqlalchemy import Column, Integer, String, FLOAT
from app import db, ma
database models:
class User(db.Model):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
email = Column(String, unique=True)
password = Column(String)
about = db.relationship('About', backref='user', lazy='dynamic')
class About(db.Model):
__tablename__ = 'abouts'
about_id = Column(Integer, primary_key=True)
description = Column(String(1000))
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
class UserSchema(ma.Schema):
class Meta:
fields = ('id', 'first_name', 'last_name', 'email', 'password')
class AboutSchema(ma.Schema):
class Meta:
fields = ('about_id', 'description')
下面是用户路由:
from flask import Blueprint, jsonify, request
from app import db
# from models.users import User, UserSchema
from models.users import User, UserSchema
from flask_jwt_extended import JWTManager, jwt_required, create_access_token
users = Blueprint('user', __name__)
user_schema = UserSchema()
users_schema = UserSchema(many=True)
@users.route('/register', methods=['POST'])
def register():
email = request.form['email']
test = User.query.filter_by(email=email).first()
if test:
return jsonify(messgae="that email already exists")
else:
first_name = request.form['first_name']
last_name = request.form['last_name']
password = request.form['password']
user = User(first_name=first_name, last_name=last_name, email=email, password=password)
db.session.add(user)
db.session.commit()
return jsonify(messgae="User created successfully"), 201
@users.route('/login', methods=['POST'])
def login():
if request.is_json:
email = request.json['email']
password = request.json['password']
else:
email = request.form['email']
password = request.form['password']
test = User.query.filter_by(email=email, password=password).first()
if test:
access_token = create_access_token(identity=email)
return jsonify(message="Login succeeded!", access_token=access_token), 200
else:
return jsonify(message="Bad email or password"), 401
这是我的数据库管理界面截图:
![图片描述](https://istack.dev59.com/QU04E.webp)