class Role(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) def __init__(self, name): self.name = name def __repr_(self): return "<Role {}>".format(self.name)
class Tag(db.Model): # __tablename__ = 'your_table_name' 通过该方法可以使用已经存在的表 id = db.Column(db.Integer(),primary_key = True) title = db.Column(db.String(255)) def __init__(self,title): self.title = title def __repr__(self): return "<Tag '{}'>".format(self.title)
class Comment(db.Model): # __tablename__ = 'your_table_name' 通过该方法可以使用已经存在的表 id = db.Column(db.Integer(),primary_key = True) name = db.Column(db.String(255)) text = db.Column(db.Text()) date = db.Column(db.DateTime()) post_id = db.Column(db.Integer(),db.ForeignKey('post.id')) def __repr__(self): return "<Comment '{}'>".format(self.text[:15])
class User(db.Model): # __tablename__ = 'your_table_name' 通过该方法可以使用已经存在的表 id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(255)) password = db.Column(db.String(255)) posts = db.relationship( 'Post', backref='user', #backref 可以使得通过Post.user 属性对User对象进行读取和修改 lazy='dynamic') roles = db.relationship('Role', secondary=roles, backref=db.backref('users', lazy='dynamic')) def __init__(self, username): self.username = username default = Role.query.filter_by(name="default").one() self.roles.append(default) def __repr__(self): return "<User '{}'>".format(self.username) def set_password(self, password): self.password = bcrypt.generate_password_hash(password) def check_password(self, password): return bcrypt.check_password_hash(self.password, password) def is_authenticated(self): if isinstance(self, AnonymousUserMixin): return False else: return True def is_active(self): return True def is_anonymous(self): if isinstance(self, AnonymousUserMixin): return True else: return False def get_id(self): return str(self.id)
class Category(db.Model): '''课程类别''' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(255), unique=True) def __init__(self, name): self.name = name def __repr__(self): return "<Category '{}'>".format(self.name)
class Student(db.Model): '''学生''' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(255)) gender = db.Column( db.String(255) ) #db.Column(Enum("female", "male", name="gender_enum", create_type=False)) birthday = db.Column(db.Date()) single_courses = db.relationship('Single_course', backref='student', lazy='dynamic') categorys = db.relationship('Category', secondary=categorys, backref=db.backref('students', lazy='dynamic')) def __init__(self, name, gender, birthday): self.name = name self.gender = gender self.birthday = birthday def __repr__(self): return "<Student '{}'>".format(self.name)
class Single_course(db.Model): ''' 一对一课程 ''' id = db.Column(db.Integer(), primary_key=True) student_id = db.Column(db.Integer(), db.ForeignKey('student.id')) teacher_id = db.Column(db.Integer(), db.ForeignKey('teacher.id')) section = db.Column( db.String(255) ) # db.Column(Enum("1", "2","3","4","5","6","7","8" ,name="section_enum", create_type=False)) date = db.Column(db.Date()) def __init__(self, date, section): self.section = section self.date = date def __repr__(self): return "<Single_course '{}-{}'>".format(self.date, self.section)
class Post(db.Model): # __tablename__ = 'your_table_name' 通过该方法可以使用已经存在的表 id = db.Column(db.Integer(),primary_key = True) title = db.Column(db.String(255)) text = db.Column(db.Text()) publish_date = db.Column(db.DateTime()) comments = db.relationship( 'Comment', backref = 'post', lazy = 'dynamic' ) user_id = db.Column(db.Integer(),db.ForeignKey('user.id')) tags = db.relationship( 'Tag', secondary = tags, backref = db.backref('posts',lazy='dynamic') ) def __init__(self,title): self.title = title def __repr__(self): return "<Post '{}'>".format(self.title)