使用数据库# 还是使用数据库扩展
pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com flask-sqlalch
emy
- 配置参数可以根据官方指导
- 定义两个表模型瞧瞧,外键需要在主表关联(一对多,约束主表)
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:123456@localhost:3306/flask_mv" # show global variables like 'port';
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
# 定义表模型
class User(db.Model):
"""
用户信息表
"""
__tablename__ = 'user' # 表名,都用小写
# 可以查看官方的例子
id = db.column(db.Integer, primary_key=True)
name = db.column(db.String(100), unique=True) # 用户名 唯一
pwd = db.column(db.String(100))
email = db.column(db.String(100), unique=True)
phone = db.column(db.String(100), unique=True)
info = db.column(db.Text)
face = db.column(db.String(255), unique=True) # 头像
addtime = db.column(db.DateTime, index=True, default=datetime.now) # 注册时间
uuid = db.column(db.String(255), unique=True) # 唯一标志符
userlogs = db.relationship('UserLog', backref='user') # 外键关系的关联
def __repr__(self):
return "<User %r>"%self.name # 定义对象的描述
class UserLog(db.Model):
"""
用户日志表
"""
__tablename__ = 'userlog'
id = db.column(db.Integer, primary_key=True)
user_id = db.column(db.Integer, db.ForeignKey('user.id')) # 外键约束 表.字段 还要在类User中关联!
ip = db.column(db.String(100))
addtime = db.column(db.DateTime, index=True, default=datetime.now)
def __repr__(self):
return '<UserLog %r>'%self.id
剩余表的模型,表之间的关系后面会在使用时说明# 电影标签
class Tag(db.Model):
__tablename__ = 'tag'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
movies = db.relationship('Movie', backref='tag')
def __repr__(self):
return '<UserLog %r>'%self.name
class Movie(db.Model):
__tablename__ = 'movie'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), unique=True) # 电影名称
url = db.Column(db.String(100), unique=True)
info = db.Column(db.Text)
logo = db.Column(db.String(255), unique=True) # 封面
star = db.Column(db.SmallInteger) # 星级
playnum = db.Column(db.BigInteger) # 播放量
commentnum = db.Column(db.BigInteger)
tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))
area = db.Column(db.String(100))
release_time = db.Column(db.Date) # 上映时间
length = db.Column(db.String(100)) # 总时长
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
comments = db.relationship('Comment', backref='movie') # 主表,关联评论表
movcoll = db.relationship('MovCollection', backref='movie') # 关联收藏表
def __repr__(self):
return '<Movie %r>'%self.title
class Preview(db.Model):
__tablename__ = 'preview'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), unique=True)
logo = db.Column(db.String(255), unique=True) # 封面
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
def __repr__(self):
return '<Preview %r>' % self.title
# 评论表
class Comment(db.Model):
__tablename__ = 'comment'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text)
movie_id = db.Column(db.Integer, db.ForeignKey('movie.id')) # 多端外键,约束主表
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 哪个用户评论的
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
def __repr__(self):
return '<Comment %r>' % self.id
# 电影收藏
class MovCollection(db.Model):
__tablename__ = 'movcollec'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text)
movie_id = db.Column(db.Integer, db.ForeignKey('movie.id')) # 多端外键,约束主表
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 哪个用户收藏的
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
def __repr__(self):
return '<MovCollection %r>' % self.id
# 权限
class Auth(db.Model):
__tablename__ = 'auth'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True) # 名称
url = db.Column(db.String(100), unique=True)
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
def __repr__(self):
return '<Auth %r>' % self.name
# 角色
class Role(db.Model):
__tablename__ = 'role'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True) # 名称
auths = db.Column(db.String(100))
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
admin = db.relationship('Admin', backref='role') # 关联管理员
def __repr__(self):
return '<Role %r>' % self.name
# 什么关系呢?
# 管理员
class Admin(db.Model):
__tablename__ = 'admin'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True) # 名称
pwd = db.Column(db.String(300)) # 这里应该是byte为单位,设计的时候就给够,如果用hash生成至少255以上
# alter table admin modify column pwd varchar(300);
is_super = db.Column(db.SmallInteger) # 0为超级管理员
role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
adminlog = db.relationship('AdminLog', backref='admin') # 关联管理员日志
operatelog = db.relationship('OperateLog', backref='admin') # 关联操作日志
def __repr__(self):
return '<Role %r>' % self.name
# 管理员日志
class AdminLog(db.Model):
__tablename__ = 'adminlog'
id = db.Column(db.Integer, primary_key=True)
admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
ip = db.Column(db.String(100)) # 登录IP
addtime = db.Column(db.DateTime, index=True, default=datetime.now)
def __repr__(self):
return '<Role %r>' % self.id
# 操作日志
class OperateLog(db.Model):
__tablename__ = 'operatelog'
id = db.Column(db.Integer, primary_key=True)
admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
ip = db.Column(db.String(100)) # 登录IP
reason = db.Column(db.String(500)) # 操作原因
addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 这个index的具体作业是?
def __repr__(self):
return '<Role %r>' % self.id
- 注意有些字段的长度要给够,这是设计阶段的问题,后期不建议修改!