简体   繁体   English

Flask-mongoengine文件编号

[英]Flask-mongoengine document id

I'm trying to implement flask-login in my application which uses flask-mongoengine too. 我正在尝试在也使用flask-mongoengine的应用程序中实现flask-login。 Here is my user proto: 这是我的用户原型:

class User(db.Document, UserMixin):
    username = db.StringField(max_length=80)
    email = db.StringField(max_length=255, unique=True)
    password = db.StringField(max_length=255, required=True)
    active = db.BooleanField(default=True)

    def __init__(self, *args, **kwargs):
        super(db.Document, self).__init__(self)
        try:
            self.username = kwargs['username']
            self.email = kwargs['email']
            self.password = kwargs['password']
        except:
            flash('Bad arguments for User')

    @staticmethod
    def salt_password(password):
        return generate_password_hash(password)

    @property
    def is_authenticated(self):
        return True

    @property
    def is_active(self):
        return self.active

    @property
    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self._id)

    def __repr__(self):
        return '<User %r>' % (self.username)

    def check_pwd(self, password):
        return check_password_hash(self.password, password)

However, when I call the login_user(user) function from my login view, it calls the get_id method of the User , but self._id returns None . 但是,当我从登录视图调用login_user(user)函数时,它将调用Userget_id方法,但self._id返回None I also tried self.id with the same result. 我也尝试了self.id ,结果相同。 Then, I tried to add the _id field explicitely : 然后,我尝试显式添加_id字段:

class User(db.Document, UserMixin):
    _id = db.ObjectIdField(default=bson.ObjectId())

but then, self._id gives me 'User u'username'> instead of the user id. 但是, self._id给了我'User u'username'>而不是用户ID。

Any idea of how to retrieve the _id of a user ? 关于如何检索用户的_id的任何想法?

I found a workaround for this issue. 我找到了解决此问题的方法。 My User prototype is updated as follow, using pymongo : 我的用户原型使用pymongo更新如下:

def get_id(self):
    user_queried = self._get_collection().find_one({'username':self.username, 'email':self.email, 'password':self.password})
    if user_queried is not None:
        return unicode(user_queried['_id'])
    else:
        return 'None'

Though I don't know if an answer to your problem is still needed - you found a solution for you and your question is old. 尽管我不知道是否仍然需要解决您的问题的方法-您为您找到了解决方案,而您的问题仍然很老。 But if someone else comes around i solved it with the following code: 但是,如果有人过来,我用以下代码解决了它:

from mongoengine import Document, StringField, FloatField, BooleanField

class User(Document):

name = StringField(primary_key=True, required=True, max_length=50)
mail = StringField(required=True, max_length=255)
password = StringField(required=True, max_length=80)
first_name = StringField(max_length=255)
second_name = StringField(max_length=255)
authenticated = True  # BooleanField(required=True, default=True)
is_active = True  # BooleanField(required=True, default=True)

meta = {'db_alias': 'users'}

@property
def is_authenticated(self):
    return True

@property
def is_active(self):
    return True

@property
def is_anonymous(self):
    return False

def get_id(self):
    return self.name

and added the following user_loader 并添加了以下user_loader

@login_manager.user_loader
def load_user(user_id):
    user = User.objects(name=user_id)[0]
    return user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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