Mongoengine AttributeError

时间:2016-07-14 00:27:21

标签: python flask mongoengine flask-mongoengine

我尝试使用Flask-Mongoengine和Flask-Login为应用编写代码。而且我得到了这个奇怪的错误:

File "/usr/lib/python3.5/site-packages/mongoengine/base/document.py", line 188, in __setattr__
    super(BaseDocument, self).__setattr__(name, value)
  File "/usr/lib/python3.5/site-packages/mongoengine/base/fields.py", line 132, in __set__
    if instance._initialised:
AttributeError: _initialised

我的models.py:

from app import app, db
from flask.ext.login import LoginManager


login_manager = LoginManager()
login_manager.init_app(app)


class User(db.Document):
    email = db.StringField(required=True)
    first_name = db.StringField(max_lenght=40, required=True)
    last_name = db.StringField(max_lenght=40, required=True)
    password = db.StringField(required=True)

    def __init__(self, email, first_name, last_name, password):
        self.email = email
        self.first_name = first_name
        self.last_name = last_name
        self.password = password

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

   def get_id(self):
        return self.email

我已经尝试过强化课程,但它并没有帮助。

https://github.com/MongoEngine/flask-mongoengine/issues/156

3 个答案:

答案 0 :(得分:3)

Mongoengine不需要你定义__init__。

TRUE

然后你所要做的就是

class User(db.Document):
    email = db.StringField(required=True)
    first_name = db.StringField(max_lenght=40, required=True)
    last_name = db.StringField(max_lenght=40, required=True)
    password = db.StringField(required=True)

    def clean(self):
        # clean will be called when you call .save()
        # You can do whatever you'd like to clean data before save
        self.password = str(self.password)

答案 1 :(得分:2)

此问题的解决方法是,您需要像这样调用超类的构造函数

class User(db.Document):
    email = db.StringField(required=True)
    first_name = db.StringField(max_lenght=40, required=True)
    last_name = db.StringField(max_lenght=40, required=True)
    password = db.StringField(required=True)

    def __init__(self, email, first_name, last_name, password, *args, **kwargs):
        super(User, self).__init__(*args, **kwargs)
        self.email = email
        self.first_name = first_name
        self.last_name = last_name
        self.password = password

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

   def get_id(self):
        return self.email

答案 2 :(得分:0)

未来的读者应该知道,在knittledan回答中,clean()每次 save()调用之前被调用,所以如果它用于哈希密码字段模型,例如:

def clean(self):
    self.password = bcrypt.generate_password_hash(self.password).decode('utf-8')

密码将被不断覆盖,这是不希望的。解决方法是使用其他字段,例如:password_hashed = db.BooleanField(default=False)

def clean(self):
    if not self.password_hashed:
        self.password        = bcrypt.generate_password_hash(self.password).decode('utf-8')
        self.password_hashed = True