我在Python中使用MongoAlchemy和Flask建立了一个MongoDB数据库。问题是,当我尝试将对象保存到数据库中时,我收到此错误,并且我在互联网上找不到答案。问题似乎与保存功能的参数安全有关,但它不应以任何方式影响它,因为该参数是可选的。这就是我的代码的样子:
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)解决了这个问题。