码迷,mamicode.com
首页 > 数据库 > 详细

python flask_Sqlalchemy管理数据库

时间:2019-01-02 13:33:20      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:email   管理   main   auth   foreign   python   uri   span   imp   

懒癌复发直接粘贴代码,算是做一个简单备份吧。

#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:toor@127.0.0.1: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()

python flask_Sqlalchemy管理数据库

标签:email   管理   main   auth   foreign   python   uri   span   imp   

原文地址:https://www.cnblogs.com/kk328/p/10208011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!