我尝试构建一个使用两种用户的flask应用程序,并且我想在这两个类之间使用current_user分开。我需要使load_user返回普通用户或交付用户。
这是加载用户功能:
@login.user_loader
def load_user(id):
return User.query.get(int(id))
这是用户类:
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
class Delivery(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
答案 0 :(得分:0)
仅使用一种用户模型并键入字段或组模型。示例:
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
group = db.Column(db.ForeignKey('group.id'), nullable=False)
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
users = db.relationship('User', backref='user', lazy=True)
description = db.Column(db.String(50))