添加 Flask 脚本任务时出现 TypeError

TypeError while adding flask script task

我正在努力修改 cookiecutter Flask 应用程序。

在基于 flask-script 的 manage.py 管理模块中,我正在尝试创建一个新任务 'create_admin' 添加管理员用户。

@manager.command
def create_admin():
    """Creates the admin user."""
    db.session.add(User(
        email="ad@min.com",
        password="admin",
        admin=True,
        confirmed=True,
        confirmed_on=datetime.datetime.now()
    )
    )
    db.session.commit()

如您所见,我已将其添加到列表末尾:

manager.add_command('server', Server())
manager.add_command('shell', Shell(make_context=_make_context))
manager.add_command('db', MigrateCommand)
manager.add_command("urls", ShowUrls())
manager.add_command("clean", Clean())
manager.add_command("create_admin", create_admin())

我有一个 'user' 模型,看起来像:

class User(UserMixin, SurrogatePK, Model):

    __tablename__ = 'users'
    username = Column(db.String(80), unique=True, nullable=True)
    email = Column(db.String(80), unique=True, nullable=False)
    #: The hashed password
    password = Column(db.String(128), nullable=True)
    created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    first_name = Column(db.String(30), nullable=True)
    last_name = Column(db.String(30), nullable=True)
    active = Column(db.Boolean(), default=False)
    is_admin = Column(db.Boolean(), default=False)
    confirmed = db.Column(db.Boolean, default=False)
    confirmed_on = db.Column(db.DateTime, nullable=True)


    def __init__(self, username, email, password=None, **kwargs):
        db.Model.__init__(self, username, email=email, **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

当我尝试 运行 新功能时:

$ python manage.py create_admin                        
Traceback (most recent call last):
  File "manage.py", line 57, in <module>
    manager.add_command("create_admin", create_admin())
  File "manage.py", line 46, in create_admin
    confirmed_on=datetime.datetime.now()
TypeError: __init__() takes at least 3 arguments (3 given)
(flask_mini)

我做错了什么?

编辑:

 File "C:\envs\r2\myflaskapp\myflaskapp\models\user.py", line 31, in __init__
    db.Model.__init__(self, username=username, email=email, **kwargs)
  File "c:\envs\virtalenvs\flask_mini\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 525, in _declarative_constructor
    (k, cls_.__name__))
TypeError: 'admin' is an invalid keyword argument for User

您的 User() class 采用 username 位置参数:

def __init__(self, username, email, password=None, **kwargs):

但你没有通过:

db.session.add(User(
    email="ad@min.com",
    password="admin",
    admin=True,
    confirmed=True,
    confirmed_on=datetime.datetime.now()
)
)

将一个作为第一个参数传入:

db.session.add(User(
    'admin',
    email="ad@min.com",
    password="admin",
    is_admin=True,
    confirmed=True,
    confirmed_on=datetime.datetime.now()
)
)

我把admin改成了is_admin;你只有一个 is_admin 字段。

请注意,您只能将字段值作为关键字参数传递给 SQLAlchemy 基础class:

db.Model.__init__(self, username=username, email=email, **kwargs)