我在 Python 中使用 MongoAlchemy 和 Flask 建立了一个 MongoDB 数据库。问题是当我尝试将一个对象保存到数据库中时,我收到了这个错误,并且我没有在互联网上找到答案。问题似乎出在 save 函数的参数safe,但它不应该以任何方式影响它,因为该参数是可选的。这就是我的代码的样子:
p1 = People(Name='Chis', Age=21, Password='abc', Vms=[
{'Name': 'Virtual1',
'Status': 'Active'},
{'Name': 'Virtual2',
'Status': 'Inactive'},
{'Name': 'Virtual3',
'Status': 'Active'}
])
p1.save()
这是完整的错误日志:
Traceback (most recent call last):
File "app.py", line 64, in <module>
app = create_app()
File "app.py", line 26, in create_app
insert_db.populateTables()
File "/home/chis/IDEs/pycharm-community-2017.1.4/Projects/REST_API/api/blueprints/db/insert_db.py", line 45, in populateTables
p1.save(safe=None)
File "/home/chis/IDEs/pycharm-community-2017.1.4/Projects/FlaskServer/env/local/lib/python2.7/site-packages/flask_mongoalchemy/__init__.py", line 275, in save
self._session.insert(self, safe=safe)
File "/home/chis/IDEs/pycharm-community-2017.1.4/Projects/FlaskServer/env/local/lib/python2.7/site-packages/mongoalchemy/session.py", line 172, in insert
self.add(item, safe=safe)
File "/home/chis/IDEs/pycharm-community-2017.1.4/Projects/FlaskServer/env/local/lib/python2.7/site-packages/mongoalchemy/session.py", line 188, in add
return self.flush()
File "/home/chis/IDEs/pycharm-community-2017.1.4/Projects/FlaskServer/env/local/lib/python2.7/site-packages/mongoalchemy/session.py", line 414, in flush
result = op.execute()
File "/home/chis/IDEs/pycharm-community-2017.1.4/Projects/FlaskServer/env/local/lib/python2.7/site-packages/mongoalchemy/ops.py", line 97, in execute
return self.collection.save(self.data, safe=self.safe)
File "/home/chis/IDEs/pycharm-community-2017.1.4/Projects/FlaskServer/env/local/lib/python2.7/site-packages/pymongo/collection.py", line 2440, in save
write_concern = WriteConcern(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'safe'
编辑:包含 People 类的脚本
from init_db import db
class People(db.Document):
Name = db.StringField()
Age = db.IntField()
Password = db.StringField()
Vms = db.AnythingField()
为未来的观众编辑:我设法通过安装旧版本的 pymongo(最好是 2.9.0)解决了这个问题。