编辑cms.models.py
from exts import db from datetime import datetime class CMSUser(db.Model): __tablename__ = 'cms_user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(50), nullable=False) password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), nullable=False, unique=True) #unique表示必须是唯一的 join_time = db.Column(db.DateTime, default=datetime.now )
把cms.models导入到manage.py中, 编辑manage.py
from apps.cms import models as cms_models
生成迁移脚本并映射到数据库中
(flask-env) D:bs>python manage.py db migrate ... (flask-env) D:bs>python manage.py db upgrade ...
给cms添加个管理用户
编辑manage.py,配置创建用户的脚本
... CMSUser = cms_models.CMSUser @manager.option('-u','--username',dest='username') @manager.option('-p','--password',dest='password') @manager.option('-e','--email',dest='email') def create_cms_user(username, password, email): user = CMSUser(username=username, password=password, email=email) db.session.add(user) db.session.commit() print('用户创建成功') ...
创建用户
(bbs) D:bs>python manage.py create_cms_user -u admin -p 123456 -e admin@bbs.com
用户创建成功
用户创建成功了,但是我们的密码却是明文的,这是非常危险的!
明文密码是万万不可以的,因此我们需要改造下models
更新模型到数据库
python manage.py db migrate
python manage.py db upgrade
再创建一个用户
(bbs) D:bs>python manage.py create_cms_user -u admin -p 123456 -e admin@heboan.com
用户创建成功