添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
胡子拉碴的豆腐  ·  Mongoose URI is ...·  1 月前    · 
逆袭的鸭蛋  ·  Delete Documents - ...·  1 月前    · 
多情的仙人球  ·  [Answered] How does ...·  4 天前    · 
礼貌的稀饭  ·  ⚙️ Customize Spring ...·  4 天前    · 
奋斗的木瓜  ·  Create DBRefs in MongoDB·  4 天前    · 
失望的烈马  ·  SQL ...·  6 月前    · 
玩命的水桶  ·  妙博客-c# winform ...·  6 月前    · 
爱喝酒的牛肉面  ·  產品碳足跡資訊網·  6 月前    · 
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.
  •