1.2数据库迁移
1、首先我们先把模型类的代码放上来
1 from datetime import datetime 2 from werkzeug.security import generate_password_hash, check_password_hash 3 4 from info import constants 5 from . import db 6 7 8 class BaseModel(object): 9 """模型基类,为每个模型补充创建时间与更新时间""" 10 create_time = db.Column(db.DateTime, default=datetime.now) # 记录的创建时间 11 update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) # 记录的更新时间 12 13 14 # 用户收藏表,建立用户与其收藏新闻多对多的关系 15 tb_user_collection = db.Table( 16 "info_user_collection", 17 db.Column("user_id", db.Integer, db.ForeignKey("info_user.id"), primary_key=True), # 新闻编号 18 db.Column("news_id", db.Integer, db.ForeignKey("info_news.id"), primary_key=True), # 分类编号 19 db.Column("create_time", db.DateTime, default=datetime.now) # 收藏创建时间 20 ) 21 22 tb_user_follows = db.Table( 23 "info_user_fans", 24 db.Column('follower_id', db.Integer, db.ForeignKey('info_user.id'), primary_key=True), # 粉丝id 25 db.Column('followed_id', db.Integer, db.ForeignKey('info_user.id'), primary_key=True) # 被关注人的id 26 ) 27 28 29 class User(BaseModel, db.Model): 30 """用户""" 31 __tablename__ = "info_user" 32 33 id = db.Column(db.Integer, primary_key=True) # 用户编号 34 nick_name = db.Column(db.String(32), unique=True, nullable=False) # 用户昵称 35 password_hash = db.Column(db.String(128), nullable=False) # 加密的密码 36 mobile = db.Column(db.String(11), unique=True, nullable=False) # 手机号 37 avatar_url = db.Column(db.String(256)) # 用户头像路径 38 last_login = db.Column(db.DateTime, default=datetime.now) # 最后一次登录时间 39 is_admin = db.Column(db.Boolean, default=False) 40 signature = db.Column(db.String(512)) # 用户签名 41 gender = db.Column( # 订单的状态 42 db.Enum( 43 "MAN", # 男 44 "WOMAN" # 女 45 ), 46 default="MAN") 47 @property 48 def password(self): 49 return "you can't read" 50 51 @password.setter 52 def password(self, value): 53 self.password_hash = generate_password_hash(value) 54 55 def check_password(self,value): 56 return check_password_hash(self.password_hash,value) 57 def check_pass(self, value): 58 pass 59 # 当前用户收藏的所有新闻 60 collection_news = db.relationship("News", secondary=tb_user_collection, lazy="dynamic") # 用户收藏的新闻 61 # 用户所有的粉丝,添加了反向引用followed,代表用户都关注了哪些人 62 followers = db.relationship('User', 63 secondary=tb_user_follows, 64 primaryjoin=id == tb_user_follows.c.followed_id, 65 secondaryjoin=id == tb_user_follows.c.follower_id, 66 backref=db.backref('followed', lazy='dynamic'), 67 lazy='dynamic') 68 69 # 当前用户所发布的新闻 70 news_list = db.relationship('News', backref='user', lazy='dynamic') 71 72 def to_dict(self): 73 resp_dict = { 74 "id": self.id, 75 "nick_name": self.nick_name, 76 "avatar_url": constants.QINIU_DOMIN_PREFIX + self.avatar_url if self.avatar_url else "", 77 "mobile": self.mobile, 78 "gender": self.gender if self.gender else "MAN", 79 "signature": self.signature if self.signature else "", 80 "followers_count": self.followers.count(), 81 "news_count": self.news_list.count() 82 } 83 return resp_dict 84 85 def to_admin_dict(self): 86 resp_dict = { 87 "id": self.id, 88 "nick_name": self.nick_name, 89 "mobile": self.mobile, 90 "register": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), 91 "last_login": self.last_login.strftime("%Y-%m-%d %H:%M:%S"), 92 } 93 return resp_dict 94 95 96 class News(BaseModel, db.Model): 97 """新闻""" 98 __tablename__ = "info_news" 99 100 id = db.Column(db.Integer, primary_key=True) # 新闻编号 101 title = db.Column(db.String(256), nullable=False) # 新闻标题 102 source = db.Column(db.String(64), nullable=False) # 新闻来源 103 digest = db.Column(db.String(512), nullable=False) # 新闻摘要 104 content = db.Column(db.Text, nullable=False) # 新闻内容 105 clicks = db.Column(db.Integer, default=0) # 浏览量 106 index_image_url = db.Column(db.String(256)) # 新闻列表图片路径 107 category_id = db.Column(db.Integer, db.ForeignKey("info_category.id")) 108 user_id = db.Column(db.Integer, db.ForeignKey("info_user.id")) # 当前新闻的作者id 109 status = db.Column(db.Integer, default=0) # 当前新闻状态 如果为0代表审核通过,1代表审核中,-1代表审核不通过 110 reason = db.Column(db.String(256)) # 未通过原因,status = -1 的时候使用 111 # 当前新闻的所有评论 112 comments = db.relationship("Comment", lazy="dynamic") 113 114 def to_review_dict(self): 115 resp_dict = { 116 "id": self.id, 117 "title": self.title, 118 "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), 119 "status": self.status, 120 "reason": self.reason if self.reason else "" 121 } 122 return resp_dict 123 124 def to_basic_dict(self): 125 resp_dict = { 126 "id": self.id, 127 "title": self.title, 128 "source": self.source, 129 "digest": self.digest, 130 "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), 131 "index_image_url": self.index_image_url, 132 "clicks": self.clicks, 133 } 134 return resp_dict 135 136 def to_dict(self): 137 resp_dict = { 138 "id": self.id, 139 "title": self.title, 140 "source": self.source, 141 "digest": self.digest, 142 "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), 143 "content": self.content, 144 "comments_count": self.comments.count(), 145 "clicks": self.clicks, 146 "category": self.category.to_dict(), 147 "index_image_url": self.index_image_url, 148 "author": self.user.to_dict() if self.user else None 149 } 150 return resp_dict 151 152 153 class Comment(BaseModel, db.Model): 154 """评论""" 155 __tablename__ = "info_comment" 156 157 id = db.Column(db.Integer, primary_key=True) # 评论编号 158 user_id = db.Column(db.Integer, db.ForeignKey("info_user.id"), nullable=False) # 用户id 159 news_id = db.Column(db.Integer, db.ForeignKey("info_news.id"), nullable=False) # 新闻id 160 content = db.Column(db.Text, nullable=False) # 评论内容 161 parent_id = db.Column(db.Integer, db.ForeignKey("info_comment.id")) # 父评论id 162 parent = db.relationship("Comment", remote_side=[id]) # 自关联 163 like_count = db.Column(db.Integer, default=0) # 点赞条数 164 165 def to_dict(self): 166 resp_dict = { 167 "id": self.id, 168 "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"), 169 "content": self.content, 170 "parent": self.parent.to_dict() if self.parent else None, 171 "user": User.query.get(self.user_id).to_dict(), 172 "news_id": self.news_id, 173 "like_count": self.like_count 174 } 175 return resp_dict 176 177 178 class CommentLike(BaseModel, db.Model): 179 """评论点赞""" 180 __tablename__ = "info_comment_like" 181 comment_id = db.Column("comment_id", db.Integer, db.ForeignKey("info_comment.id"), primary_key=True) # 评论编号 182 user_id = db.Column("user_id", db.Integer, db.ForeignKey("info_user.id"), primary_key=True) # 用户编号 183 184 185 class Category(BaseModel, db.Model): 186 """新闻分类""" 187 __tablename__ = "info_category" 188 189 id = db.Column(db.Integer, primary_key=True) # 分类编号 190 name = db.Column(db.String(64), nullable=False) # 分类名 191 news_list = db.relationship('News', backref='category', lazy='dynamic') 192 193 def to_dict(self): 194 resp_dict = { 195 "id": self.id, 196 "name": self.name 197 } 198 return resp_dict
2、有了这些代码后我们在pychram中打开命令框
然后输入python manage.py mysql init这行代码(注意:输入前记得看好文件的路径),为什么是mysql init,因为我们在manage中设置的关于
数据库操作代码就是这个,(根据自己的修改输入代码
这个代码操作成功会出现一堆操作的提示,最后出现done就说明成功了,然后会生成一下的文件
最后在命令框中输入python manage.py mysql migrations 来完成最后的迁移步骤,连没连接成功可以通过
pychrm中的Database 查看是否和数据库连接成功,成功了就会显示出你数据库中的数据