flask 初始化DB指令函数(flask 97)

from flask_script import Manager app = Flask(name) basedir = os.path.abspath(os.path.dirname(file)) manager=Manager(app)

def register_commands(app): @manager.option('--drop', default=True, help='Create after drop.') def initdb(drop): """Initialize the database.""" if drop: click.confirm('This operation will delete the database, do you want to continue?', abort=True) db.drop_all() print('Drop tables.') db.create_all() Role.init_role() print('Initialized database.')

@manager.option('--user', default=10, help='Quantity of users, default is 10.')
@manager.option('--follow', default=30, help='Quantity of follows, default is 30.')
@manager.option('--photo', default=30, help='Quantity of photos, default is 30.')
@manager.option('--tag', default=20, help='Quantity of tags, default is 20.')
@manager.option('--collect', default=50, help='Quantity of collects, default is 50.')
@manager.option('--comment', default=100, help='Quantity of comments, default is 100.')
def forge(user, follow, photo, tag, collect, comment):
    """Generate fake data."""

    from fakes import fake_admin, fake_comment, fake_follow, fake_photo, fake_tag, fake_user, fake_collect

    db.drop_all()
    db.create_all()

    print('Initializing the roles and permissions...')
    Role.init_role()
    print('Generating the administrator...')
    fake_admin()
    print('Generating %d users...' % user)
    fake_user(user)
    print('Generating %d follows...' % follow)
    fake_follow(follow)
    print('Generating %d tags...' % tag)
    fake_tag(tag)
    print('Generating %d photos...' % photo)
    fake_photo(photo)
    print('Generating %d collects...' % photo)
    fake_collect(collect)
    print('Generating %d comments...' % comment)
    fake_comment(comment)
    print('Done.')

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券