添加自定义命令
下面是一个使用命令行添加管理员账号的伪代码
from info.models import User
from flask_script import Manager
app = Flask(__neme__)
manage = Manager(app)
# 添加自定义命令
@manage.option('-n', '-name', dest='name')
@manage.option('-p', '-password', dest='password')
def createadmin(name, password):
if not all([name, password]):
print('参数不全')
user = User()
user.mobile = name
user.nick_name = name
user.password = password
user.is_admin = 1
try:
db.session.add(user)
db.session.commit()
except Exception as e:
print(e)
print('创建失败')
db.session.rollback()
return
print('创建管理员用户成功')
# 使用示例(命令行,manage.py存在的路径)
python manage -n admin_user -p asd258852