Issue
I am trying to create a college system using flask but I have a problem with log in system basically I have two classes Teacher class and Student class the problem is that I cant figure a way to make the login manager handle the two classes login at the same time only one can log in and when the other class try to log in it log me as the class what I mean is when I setup the login manager to accept student it works with the student but when I try to login as a teacher it searches in the student table instead of teacher table I don't know if I explained it right but i hope code snippet help
this is models
attend = db.Table('attend',
db.Column('teacher_id',db.Integer,db.ForeignKey('teacher.id')),
db.Column('student_id', db.Integer, db.ForeignKey('student.id'))
)
class Teacher(db.Model,UserMixin):
id = db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(60),nullable=False)
email=db.Column(db.String(100),nullable=False,unique=True)
password=db.Column(db.String(60),nullable=False)
department=db.Column(db.String(50),nullable=False)
students=db.relationship('Student',secondary=attend,backref='students',lazy='dynamic')
def type_auth(self):
return True
def __repr__(self) -> str:
return f'name:{self.name} email:{self.email} department:{self.department}'
class Student(db.Model,UserMixin):
id = db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(60),nullable=False)
email = db.Column(db.String(100), nullable=False, unique=True)
password = db.Column(db.String(60), nullable=False)
year=db.Column(db.Integer,nullable=False)
grades = db.relationship('Grades', backref='Rates', lazy='dynamic')
def type_auth(self):
return False
def __repr__(self) -> str:
return f'name:{self.name} email:{self.email} year:{self.year}'
this login manager
def create_app():
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']=f'sqlite:///{DB_NAME}'
app.config['SECERT_KEY'] = '1853d2c8983cff3b'
app.config['FLASK_ADMIN_SWITCH']='Cyborg'
db.init_app(app)
from .auth import auth
from .routes import routes
app.register_blueprint(auth,prefix_url='/')
app.register_blueprint(routes,prefix_url='/')
login_manger=LoginManager()
login_manger.login_view='auth.login'
login_manger.login_message_category='info'
login_manger.init_app(app)
@login_manger.user_loader
def user_loader(id):
#i need help here
return app
Solution
The user_loader
callback received an id
as an argument and from that information is expected to return an instance of whatever you have defined as your "User" class. You have effectively defined two "User" classes, and in the current configuration there is no way for the user loader, if passed "1", to determine whether it should return the Teacher
with id 1 or the Student
with id 1.
A better design might be to move email
and password
into a new User
class, and create one-to-one relationships between User
and Student
and Teacher
. You can add some validation to ensure that a User
cannot relate to both a Student
and a Teacher
.
Answered By - snakecharmerb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.