python flask_Sqlalchemy管理資料庫
阿新 • • 發佈:2019-01-02
懶癌復發直接貼上程式碼,算是做一個簡單備份吧。
#coding:utf8 from flask import Flask from flask_sqlalchemy import SQLAlchemy from datetime import datetime import pymysql app=Flask(__name__) #例項化 app.config["SQLALCHEMY_DATABASE_URI"]="mysql+pymysql://root:[email protected]:3307/movie" app.config["SQLALCHEMY_TRACK_MODIFICATION"]=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(11),unique=True) info=db.Column(db.Text) face=db.Column(db.String(255)) addtime=db.Column(db.DateTime,index=True,default=datetime.now) 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') 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')) ip=db.Column(db.String(100)) #登入ip. 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 "<Tag %r>" % self.title #電影 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) 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(100)) addtime=db.Column(db.DateTime, index=True, default=datetime.now) comments = db.relationship('Comment', backref='movie') moviecols = db.relationship('Moviecol', backref='movie') def __repr__(self): return "<movie %r>" % self.title class Preview(db.Model): 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 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.now) def __repr__(self): return "<Moviecol %r>" % self.id #許可權 class Auth(db.Model): __tablename__="auth" id=db.Column(db.Integer,primary_key=True) name=db.Column(db.String(100),unique=True) url=db.Column(db.String(255),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(100),unique=True) auths=db.Column(db.String(600)) addtime=db.Column(db.DateTime, index=True, default=datetime.now) admins=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(100), unique=True) pwd = db.Column(db.String(100)) is_super=db.Column(db.SmallInteger) role_id=db.Column(db.Integer,db.ForeignKey('role.id'))#所屬角色 ddtime = db.Column(db.DateTime, index=True, default=datetime.now) adminlog=db.relationship("Adminlog",backref='admin') #管理員外來鍵關聯 Oplogs = db.relationship("Oplog", backref='admin') def __repr__(self): return "<Admin %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 "<adminlog %r>" % self.id #操作日誌 class Oplog(db.Model): __tablename__="oplog" 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(600)) #操作原因 addtime=db.Column(db.DateTime,index=True,default=datetime.now) def __repr__(self): return "<adminlog %r>" % self.id if __name__ =='__main__': #db.create_all() """ role=Role( name="超級管理員", auths="" ) db.session.add(role) db.session.commit() """ from werkzeug.security import generate_password_hash admin=Admin( name='kk', pwd=generate_password_hash("kk"), is_super=0, role_id=1 ) db.session.add(admin) db.session.commit()