グループテーブルにユーザを追加したいのですが、すべてのグループメンバーでユーザをチェックしています。もしすでにテーブルにいたら、メッセージをフラッシュし、グループ。それは最初のユーザーのためだけです。あなたがuser_id=user_id
だけな記録とclassroomMembers
レコードに取得データベースに複数のユーザを追加することはできません
classroomMembers = ClassroomMember.query.filter_by(user_id=user_id).all()
を要求すると、残りはロジックが
@main.route('/join-classroom/<int:classroom_id>/<int:user_id>/')
@login_required
def join_classroom(classroom_id, user_id):
classroom = Classroom.query.filter_by(id=classroom_id).first()
if classroom is None:
flash('Invalid classname')
return redirect(url_for('main.classrooms'))
else:
# check if user aleady added or on request
classroomMembers = ClassroomMember.query.filter_by(user_id=user_id).all()
for classroomMember in classroomMembers:
if (classroomMember.user_id == user_id) and (classroomMember.classroom_id == classroom_id):
flash('You are already in Group ')
return redirect(url_for('main.classrooms'))
elif (classroomMember.user_id != user_id) and (classroomMember.classroom_id != classroom_id):
classroomMember = ClassroomMember(classroom_id=classroom_id, user_id=user_id)
db.session.add(classroomMember)
db.session.commit()
flash('Your request has been sent successfully')
return redirect(url_for('main.classrooms'))
elif classroomMember.user_id is None:
classroomMember = ClassroomMember(classroom_id=classroom_id, user_id=user_id)
db.session.add(classroomMember)
db.session.commit()
flash('Your request has been sent successfully')
return redirect(url_for('main.classrooms'))
私はこの問題を解決しました。 –