I am working with mongodb and python flask. Every time I run flask I receive this error on the local host and the terminal query = db.Data.find(upsert=True) AttributeError: ' NoneType' object has no attribute ' Data' What I have tried: <pre>app = Flask(__name__) app.config[ ' MONGO_URI' ] = ' mongodb://****/****@cluster0-shard-00-00.i5hec.mongodb.net:27017,cluster0-shard-00-01.i5hec.mongodb.net:27017,cluster0-shard-00-02.i5hec.mongodb.net:27017/?ssl=true&replicaSet=atlas-oxpqgz-shard-0&authSource=admin&retryWrites=true&w=majority' app.config[ ' MONGO_DBNAME' ] = ' Apitest1' mongo = PyMongo(app) db = mongo.db if __name__ == ' __main__' : app.run(debug=True ,port= 8080 ,use_reloader=False) @app.route( ' /find' , methods=[ ' GET' ]) def findAll(): # db.Data.upsert() query = db.Data.find(upsert=True) output = {} i = 0 for x in query: output[i] = x output[i].pop( ' _id' ) i += 1 return jsonify(output) @app.route( ' /insert-one/<name>/<id>/' , methods=[ ' GET' ]) def insertOne(name, id): # db.Data.upsert() queryObject = { ' Name' : name, ' ID' : id query = db.Data.insert_one(queryObject, upsert=True) return " Query inserted...!!!"
i added this

def get_db():
"""
Configuration method to return db instance
"""
db = getattr(g, current_app.config['MONGO_DBNAME'])

if db is None:
db = g._database = PyMongo(current_app).db

return db



db = LocalProxy(get_db)


but now i get this error:
File "C:\Users\CucumisSativus\PycharmProjects\api\application.py", line 32, in findAll
query = db.Data.find(upsert=True)
File "c:\users\cucumissativus\pycharmprojects\api\venv\lib\site-packages\werkzeug\local.py", line 316, in __get__
obj = instance._get_current_object() # type: ignore[misc]
File "c:\users\cucumissativus\pycharmprojects\api\venv\lib\site-packages\werkzeug\local.py", line 520, in _get_current_object
return get_name(local()) # type: ignore
File "C:\Users\CucumisSativus\PycharmProjects\api\application.py", line 17, in get_db
db = getattr(g, current_app.config['MONGO_DBNAME'])
File "c:\users\cucumissativus\pycharmprojects\api\venv\lib\site-packages\flask\ctx.py", line 52, in __getattr__
raise AttributeError(name) from None
AttributeError: Apitest1
i tried this:

def get_db():
"""
Configuration method to return db instance
"""
db = getattr(g, "_Apitest1", None)

if db is None:
db = g._database = PyMongo(current_app).db

return db


# Use LocalProxy to read the global db instance with just `db`
db = LocalProxy(get_db)

but it gave me :

File "C:\Users\CucumisSativus\PycharmProjects\api\application.py", line 32, in findAll
query = db.Data.find(upsert=True)Open an interactive python shell in this frame
AttributeError: 'NoneType' object has no attribute 'Data'
There is obviously something wrong with your code, and it is impossible to figure out what from here. If you cannot use the Python debugger then you need to add some print or logging statements to find out which of your references are not being set. Also take a look at the section on database access in the link I gave you.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •