3. Database Model Design 3.1 building Blueprint Project directory
1. front and rear project directory analysis
2. Blueprint Build project directory
Blueprint: An application or cross-domain application to make components and support common patterns.
The role of blueprints: modularity of different functions
Building Large applications
Optimize project structure
Improved readability and ease of maintenance
- Defining Blueprints (app/admin/__init__.py)
From flask Import Blueprint
admin = Blueprint ("admin", __name__)
Import views
- Registration Blueprint (app/__init__.py)
From admin import admin as Admin_blueprint
App.register_blueprint (admin_blueprint,url_prefix= "/admin")
- Call Blueprint (app/admin/views.py)
From. Import Admin
@admin. Route ("/")
3. member and Member login log data Model design
- To install a database connection dependency package
Pip Install Flask-sqlalchemy
- Defining Database connections
From flask import Flask
From Flask_sqlalchemy import SQLAlchemy
App = Flask (__name__)
app.config["Sqlalchemy_database_uri"] = "Mysql://root:[email protected]:3306" #换成自己的数据库名字
app.config["sqlalchemy_track_modifications"] = True
db = SQLAlchemy (APP)
- Define a member data model
ID: Number, Name: Account, pwd: password, email: email, phone: mobile number, Info: Profile, face: Avatar
Addtime: Registration time, UUID: Unique identifier, comments: Comment Foreign Key Association, userlogs: Member Login log foreign Key association, Moviecols: Movie Collection foreign Key Association
Class User (db. Model):
__tablename__ = "User"
ID = db. Column (db. Integer, Primary_key=true) #编号
Name = db. Column (db. String (+), unique=true) #昵称
PWD = db. Column (db. String (+)) #密码
email = db. Column (db. String (+), unique=true) #邮箱
Phone = db. Column (db. String (one), unique=true) #手机号码
info = db. Column (db. Text) #个性简介
face = db. Column (db. String (255), unique=true) #头像
Addtime = db. Column (db. Datetime,index=true,default=datetime.utcnow)
UUID = db. Column (db. String (255), unique=true) #唯一标识符
Userlogs = db.relationship (' Userlog ', backref = ' user ') #会员日志外键关系
Member login Log :
ID: number,
USER_ID: Member Number,
IP: Recently logged in IP address,
Addtime: Last Logon time
Define Tags:
ID: number, Name: Title, Movies: Movie foreign Key Association, Addtime: Add time
movie :
ID: number, title: movie title, URL: Movie address, Info: Movie profile, Logo: Movie cover, Star: Stars,
Playnum: Movie volume, Commentnum: Film Commentary, tag_id: Tag, area: region,
Release_time: Release time, Length: Movie length, Addtime: Add time, Comments: Movie Reviews foreign Key Association, Moviecols: Movie Collection foreign Key Association,
Release Notice :
ID: Number
Name: Release Trailer title
Logo: Release Trailer cover
Addtime: Add Time
Comments :
ID: Number
Content: Comments
movie_id: Belongs movie
USER_ID: Owning User
Addtime: Last Logon time
Favorite Movies:
ID: Number
movie_id: Belongs movie
USER_ID: Owning User
Addtime: Last Logon time
Permissions:
ID: Number
Name: No.
URL: Address
Addtime: Creation Time
Role:
ID: Number
Name: Names
Auths: Permissions List
Addtime: Creation Time
Administrator:
ID: Number, Name: Administrator name, pwd: Administrator password, Is_super: Whether Super Administrator, role_id: Role number, Addtime: Creation time, Adminlogs: admin logon log foreign Key association, Oplogs: Action Log Foreign Key association.
Administrator login log:
ID: number, admin_id: belongs to admin number, IP: Last login address, Addtime: Add time
Operation log:
ID: number, admin_id: belongs to the administrator number, IP: Ah oh do address, Reason: Operation Reason, Addtime: Add time
models.py
#coding: Utf-8from flask Import flaskfrom flask_sqlalchemy import sqlalchemyfrom _datetime import Datetimeimport pymysql# Define database Connection app = Flask (__name__) #创建实例化app对象app. config["Sqlalchemy_database_uri"] = "mysql+pymysql://root:[email Protected]:3306/movie "app.config[" sqlalchemy_track_modifications "] = True #配置, if set to True, will track object modification and send signal db = SQLAlchemy (APP) #定义db, incoming App object # defines the member data Model class User (db. Model): __tablename__ = "user" #存入表名称 #column字段 unique Unique ID = db. Column (db. Integer, primary_key=true) #编号 name = db. Column (db. String (+), unique=true) #昵称 pwd = db. Column (db. String) #密码 email = db. Column (db. String (+), unique=true) #邮箱 phone = db. Column (db. String (one), unique=true) #手机号码 info = db. Column (db. Text) #个性简介 face = db. Column (db. String (255), unique=true) #头像 addtime = db. Column (db. Datetime,index=true,default=datetime.utcnow) UUID = db. Column (db. String (255), unique=true) #唯一标识符 userlogs = db.relationship (' Userlog ', backref= ' user ') #会员日志外键关系 Comments = db.relationship (' Comment ', backref= ' user ') #评论外键关系 moviecols = db.relationship (' Moviecol ', backref= ' user ') # Collection foreign key Relationship #定义一个方法, return type def __repr__ (self): return "<user%r>"% self.name# Member Login log class Userlog (db. Model): __tablename__ = "Userlog" #定义表名 id = db. Column (db. integer,primary_key=true) #编号 #定义外键 db. ForeignKey user_id = db. Column (db. Integer,db. ForeignKey (' user.id ')) #所属会员 IP = db. Column (db. String (+)) #登录IP Addtime = db. Column (db. Datetime,index=true,default=datetime.utcnow) #登录时间, default time def __repr__ (self): return ' <userlog%r> '% self. id# defines the tag class tag (db). Model): __tablename__ = "tag" #定义表名称 ID = db. Column (db. integer,primary_key=true) #编号 name = db. Column (db. String (+), unique=true) #名称 addtime = db. Column (db. Datetime,index=true,default=datetime.utcnow) #添加时间 movies = Db.relationship ("Movie", backref= ' tag ') #电影外键关系关联 #返回类型 def __repr__ (self): return "<tag%r>"%self.name# film 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 (255), unique=true) #地址 info = db. Column (db. Text) #简介 logo = db. Column (db. String (255), unique=true) #封面 star = db. Column (db. Smallinteger) #星级 small shaping 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 (255)) #上映地区 Release_time = db. Column (db. Date) #上映时间 length = db. Column (db. String (+)) #播放时间 Addtime = db. Column (db. Datetime,index=true, Default=datetime.utcnow) #添加时间 comments = Db.relationship ("Comment", backref= ' movie ') #评论外键关系关联 Moviecols = Db.relationship ("Moviecol", backref= ' movie ') #收藏外键关系关联 def __repr__ (self): return "<movie%r>" %self.title# Release Preview 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.utcnow) #添加时间 def __repr__ (self): return "<preview%r>"%self.titl e# Reviews 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.utcnow) #添加时间 def __repr__ (self): return "<comment%r>"%self.id# power The Shadow Collection Class Moviecol (db. Model): __tablename__ = "Moviecol" #定义表名 id = db. Column (db. integer,primary_key=true) #编号 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.utcnow) #添加时间 def __repr__ (self): return "<moviecol%r>"% self.id# Permissions class Auth ( Db. Model): __tablename__ = "Auth" #定义表名 id = db. Column (db. integer,primary_key=true) #编号 name = db. Column (db. String (+), unique=true) #名称 URL = db. Column (db. String (255), unique=true) #地址 addtime = db. Column (db. Datetime,index=true,default=datetime.utcnow) #添加时间 def __repr__ (self): return "<auth%r>"% self.name# angle The Color class Role (db. Model): __tablename__ = "role" #定义表名 ID = db. Column (db. integer,primary_key=true) #编号 name = db. Column (db. String (+), unique=true) #名称 auths = db. Column (db. String (addtime) = db. Column (db. Datetime,index=true,default=datetime.utcnow) #添加时间 def __repr__ (self): return "<role%r>"% self.name# Tube Manager Class Admin (db. Model): __tablename__ = "admin" #存入表名称 ID = db. Column (db. Integer, primary_key=true) #编号 name = db. Column (db. String (+), unique=true) #管理员账号 pwd = db. Column (Db. String (+)) #管理员密码 Is_super = db. Column (db. Smallinteger) #是否为超级管理员, 0 for super Administrator role_id = db. Column (db. Integer,db. ForeignKey (' role.id ')) #所属角色 addtime = db. Column (db. Datetime,index=true,default=datetime.utcnow) #添加时间 adminlogs = db.relationship ("Adminlog", backref= ' admin ') #管理员登录日志 FOREIGN Key Relationship association oplogs = db.relationship ("Oplog", backref= ' admin ') #管理员操作日志外键关系关联 def __repr__ (self): return "<role %r> "% self.name# admin Log in log Class Adminlog (db. Model): __tablename__ = "Adminlog" #定义表名 id = db. Column (db. integer,primary_key=true) #编号 #定义外键 db. ForeignKey admin_id = db. Column (db. Integer,db. ForeignKey (' admin.id ')) #所属管理员 IP = db. Column (db. String (+)) #登录IP Addtime = db. Column (db. Datetime,index=true,default=datetime.utcnow) #登录时间, default time def __repr__ (self): return ' <adminlog%r> '% self . id# operation Log Class Oplog (db. Model): __tablename__ = "Oplog" #定义表名 id = db. Column (db. integer,primary_key=true) #编号 #定义外键 db. ForeignKey admin_id = db. ColuMN (db. Integer,db. ForeignKey (' admin.id ')) #所属管理员 IP = db. Column (db. String (+)) #登录IP reason = db. Column (db. String ()) #操作原因 addtime = db. Column (db. Datetime,index=true,default=datetime.utcnow) #登录时间, default time def __repr__ (self): return "<oplog%r>"% self.id if __name__ = = "__main__": #db. Create_all () "Role = Role (name=" Super admin ", auths=" ") db.session . Add (role) Db.session.commit () ' from werkzeug.security import generate_password_hash #导入一个生成密码的工具 admin = admi N (name= "Flower", Pwd=generate_password_hash ("123456"), is_super=0, role_id=1) #调用admin Db.session.add (Admin) db.session.commit ()
flask-Database Model Design 2