Cristian Coroian
Cristian Coroian

Reputation: 71

How to resolve: TypeError: __init__() got an unexpected keyword argument 'safe'

I set up a MongoDB database, using MongoAlchemy and Flask in Python. The problem is that when I try to save an object into the database, I get this error, and I didn't found an answer on the internet. The problem seems to be with the parameter safe of the save function, but it shouldn't affect it in any way, because that parameter is optional. This is how my code looks:

    p1 = People(Name='Chis', Age=21, Password='abc', Vms=[

            {'Name': 'Virtual1',
             'Status': 'Active'},

            {'Name': 'Virtual2',
             'Status': 'Inactive'},

            {'Name': 'Virtual3',
             'Status': 'Active'}
        ])

    p1.save()

This is the full error log:

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'

Edit: script containing the People class

from init_db import db


class People(db.Document):
    Name = db.StringField()
    Age = db.IntField()
    Password = db.StringField()
    Vms = db.AnythingField()

Edit for future viewers: I managed to solve the issue by installing an older version of pymongo (preferably 2.9.0).

Upvotes: 3

Views: 2927

Answers (0)

Related Questions