2016-07-14 21 views
0

Ich versuche die Autorisierung für App mit Flask-Mongoengine und Flask-Login zu kodieren. Und ich bin immer diese seltsamen Fehler:Mongoengine AttributeError

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 

Mein 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 

Ich habe versucht, die Klasse supering, aber es half nicht.

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

+0

Haben Sie aktualisieren oder auf dieses Problem beheben? – Tronic

Antwort

0

Die Fehlerbehebung für dieses Problem ist, dass Sie den Konstruktor der Superklasse wie diese

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(db.Document, 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 
0

Mongoengine aufrufen müssen nicht Sie benötigen einen __init__ zu definieren.

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) 

Dann alles, was Sie tun müssen, ist

user = User(email='[email protected]', first_name='hello', last_name='there', password=2342143213) 
user.save() 
print(user.id)