Python3 Flask Model Class Init

时间:2017-08-23 15:12:58

标签: python python-3.x flask mongoengine

我无法找到如何初始化Model类...

# Define mongoengine documents
class User(db.Document, UserMixin):
    name = db.StringField(max_length=75,primary_key=True)
    email = db.EmailField(max_length=255,unique=True,required=True)
    password = db.StringField()
    created_on = db.DateTimeField(
        default=datetime.datetime.now, required=True
    )
    isActive = db.BooleanField(default=True)
    isAuthenticated = db.BooleanField(default=False)
    isAdmin = db.BooleanField(default=False, required=False)

    meta = {'collection': 'users'}

    def __init__(self,name,email,password,created_on,isActive=True,isAuthenticated=False,isAdmin=False):
        self.name=name
        self.email=email
        self.password = bcrypt.generate_password_hash(
            password, app.config.get('BCRYPT_LOG_ROUNDS')
        ).decode('utf-8')
        self.created_on==datetime.datetime.now()
        self.isActive = isActive
        self.isAuthenticated = isAuthenticated
        self.isAdmin = isAdmin

我收到以下错误:

TypeError: __init__() missing 4 required positional arguments: 'name', 'email', 'password', and 'created_on'

我不确定我缺少什么,语法似乎是正确的。

当我删除 init 功能

时,该模型有效(使用无密码的密码)

这是Python3与Python2之间的问题吗?

完整追溯:[https://gist.github.com/642ad5c9d86a3bb8da70757636424209]

1 个答案:

答案 0 :(得分:1)

我认为您正在将模型对象创建为user = User(),而您应该user = User(name="Abc",email="abc@def.com", password="password!",created_on=datetime.datetime.now()),因为您已将这4个参数设为required

TypeError: __init__() missing 4 required positional arguments: 'name', 'email', 'password', and 'created_on'

请您更新OP以了解如何创建用户对象