简体   繁体   中英

Python3 Flask Model Class Init

I can't find out how to init a Model class ...

# 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

I get the following error:

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

I'm not sure what I'm missing, the syntax seems correct.

The model works (with unsalted password) when I remove the init function

Is this a Python3 vs Python2 kind of issue?

Full traceback: [ https://gist.github.com/642ad5c9d86a3bb8da70757636424209]

I suppose you are creating the model object as user = User() instead you should probably do user = User(name="Abc",email="abc@def.com", password="password!",created_on=datetime.datetime.now()) since you have made those 4 arguments as required

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

Could you please update the OP with how you are creating the User obj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM