python flask_Sqlalchemy管理数据库

Posted kailicard

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python flask_Sqlalchemy管理数据库相关的知识,希望对你有一定的参考价值。

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

#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()

以上是关于python flask_Sqlalchemy管理数据库的主要内容,如果未能解决你的问题,请参考以下文章

Flask 学习-19.配置管理flask_sqlalchemy 和 flask_migrate

flask_sqlalchemy简单用法

ModuleNotFoundError:没有名为“flask_sqlalchemy.orm”的模块

深入浅出Flask(48):flask_sqlalchemy的创建表

No module named 'flask_sqlalchemy'

from flask_sqlalchemy import SQLAlchemy 无法创建数据库表,代码无报错,代码如下,python版本是3.5