Python 更新';用户名';字段注销用户(flask登录-MongoDB)-类问题?

Python 更新';用户名';字段注销用户(flask登录-MongoDB)-类问题?,python,mongodb,flask,flask-login,Python,Mongodb,Flask,Flask Login,我正在开发我的flask应用程序(使用flask登录名和PyMongo),为我的用户和这个加载程序创建了这个类(这是完整的users.py文件): 注册、登录和注销路径工作正常,但我的更新帐户路径有问题,它会更新用户信息。 我可以更新数据库中的所有字段,但当我更新'username'时,我会注销,或者更确切地说是注销并重定向到/login?next=%2fcount HTTP(尽管该字段已正确更新)。 我知道我的类中有错误,所以我尝试使用\u id修改它的结尾和加载程序,例如: class Us

我正在开发我的flask应用程序(使用flask登录名和PyMongo),为我的用户和这个加载程序创建了这个类(这是完整的users.py文件):

注册、登录和注销路径工作正常,但我的更新帐户路径有问题,它会更新用户信息。 我可以更新数据库中的所有字段,但当我更新
'username'
时,我会注销,或者更确切地说是注销并重定向到
/login?next=%2fcount HTTP
(尽管该字段已正确更新)。 我知道我的类中有错误,所以我尝试使用
\u id
修改它的结尾和加载程序,例如:

class User():
    def __init__(self, username, first_name, last_name, email, _id, is_admin):
        self.username = username
        self.first_name = first_name
        self.last_name = last_name
        self.email = email
        self._id = _id
        self.is_admin = is_admin


    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self._id

@login.user_loader
def load_user(_id):
    user = mongo.db.users.find_one({'_id': _id})
    if not user:
        return None
    return User(user['username'], user['first_name'], user['last_name'], user['email'], user['_id'] , user['is_admin'])
但是我得到了一个
TypeError:ObjectId类型的对象不是JSON可序列化的

额外信息:当我更新控制台中正确获取的任何其他字段时:

127.0.0.1 - -"POST /account HTTP/1.1" 302 -
127.0.0.1 - -"GET /account HTTP/1.1" 200 -
但更新用户名会引发:

127.0.0.1 - -"POST /account HTTP/1.1" 302 -
127.0.0.1 - -"GET /account HTTP/1.1" 302 -
127.0.0.1 - -"GET /login?next=%2Faccount HTTP/1.1" 200 -
如果有帮助,这是我编辑帐户的途径:

@app.route('/account', methods=['POST', 'GET'])
@login_required
def account():
    form = UpdateAccountForm()
    updated_user = {'username': form.username.data, 'first_name': form.first_name.data,
                    'last_name': form.last_name.data, 'email': form.email.data}
    if form.validate_on_submit():
        mongo.db.users.update_one(
            {'_id': current_user._id}, {"$set": updated_user})
        flash('Updated!', 'info')
        return redirect(url_for('account'))
    elif request.method == 'GET':
        form.username.data = current_user.username
        form.first_name.data = current_user.first_name
        form.last_name.data = current_user.last_name
        form.email.data = current_user.email
    return render_template('account.html', title="About", form=form)

你们谁能帮我整理一下吗?谢谢

pymongo中将
\u id
字段表示为ObjectId实例(来自
bson
库)


要么从有问题的函数中删除此字段,要么先将其转换为字符串。

我通过在同一路径中更新此字段时立即登录用户来修复此问题。因此,这是一个后端技巧,但用户无法察觉。

我尝试将其转换为字符串,但没有成功。你有什么建议?另外,是否可以将其从我的功能中删除?我想我需要它。非常感谢您的帮助!我需要查看函数。我刚刚更新了我的问题,但在我的users.py中没有更多内容。我还在问题的末尾添加了路由,以防万一。如果您愿意,我可以与您共享我的存储库和实时部署,以便您了解问题所在。嗨,Guillermo,我也在看一些flask登录MongoDB工作片段,我希望您能分享它的工作原理。有回购协议吗?
@app.route('/account', methods=['POST', 'GET'])
@login_required
def account():
    form = UpdateAccountForm()
    updated_user = {'username': form.username.data, 'first_name': form.first_name.data,
                    'last_name': form.last_name.data, 'email': form.email.data}
    if form.validate_on_submit():
        mongo.db.users.update_one(
            {'_id': current_user._id}, {"$set": updated_user})
        flash('Updated!', 'info')
        return redirect(url_for('account'))
    elif request.method == 'GET':
        form.username.data = current_user.username
        form.first_name.data = current_user.first_name
        form.last_name.data = current_user.last_name
        form.email.data = current_user.email
    return render_template('account.html', title="About", form=form)