{"id":205055,"date":"2025-05-29T08:46:56","date_gmt":"2025-05-29T00:46:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205055\/"},"modified":"2025-05-29T08:46:56","modified_gmt":"2025-05-29T00:46:56","slug":"flask-sqlalchemy-%e5%b7%a5%e7%a8%8b%e5%8c%96%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8dthe-setup-method-shell_context_processor-can-no-longer-be-called-on-the-application","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205055\/","title":{"rendered":"Flask-SQLAlchemy \u5de5\u7a0b\u5316\u65f6\uff0c\u5982\u4f55\u907f\u514d\\&#8221;The setup method \\&#8217;shell_context_processor\\&#8217; can no longer be called on the application\\&#8221;\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Flask-SQLAlchemy \u5de5\u7a0b\u5316\u65f6\uff0c\u5982\u4f55\u907f\u514d\\&#8221;The setup method \\&#8217;shell_context_processor\\&#8217; can no longer be called on the application\\&#8221;\u9519\u8bef\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/173225903067402cd6e3684.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>flask-sqlalchemy \u5de5\u7a0b\u5316\u9047\u5230\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u5b98\u65b9\u6587\u6863\u4e2d\u4f7f\u7528 app.py \u76f4\u63a5\u521d\u59cb\u5316\u6570\u636e\u5e93\u7684\u793a\u4f8b\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e76\u4e0d\u53ef\u884c\u3002\u5c06\u6570\u636e\u5e93\u521d\u59cb\u5316\u4ee3\u7801\u5c01\u88c5\u5230\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u5e76\u5f15\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u9519\u8bef\uff1a<\/p>\n<pre>nexpect system error - the setup method 'shell_context_processor' can no longer be called on the application. it has already handled its first request, any changes will not be applied consistently.\nmake sure all imports, decorators, functions, etc. needed to set up the application are done before running it.<\/pre>\n<p>\u8be5\u9519\u8bef\u63d0\u793a\u8868\u660e flask \u5e94\u7528\u7a0b\u5e8f\u5df2\u7ecf\u5904\u7406\u4e86\u7b2c\u4e00\u4e2a\u8bf7\u6c42\uff0c\u65e0\u6cd5\u518d\u8fdb\u884c\u914d\u7f6e\u66f4\u6539\u3002\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u5f15\u5165\u6570\u636e\u5e93\u4ee3\u7801\u540e\uff0c\u56e0\u4e3a current_app \u548c current_request \u7b49 current \u5bf9\u8c61\u4ec5\u5b58\u5728\u4e8e flask \u5e94\u7528\u7a0b\u5e8f\u7684\u4e0a\u4e0b\u6587\u4e2d\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u9700\u8981\u5728\u6587\u4ef6\u4e2d\u4f7f\u7528\u81ea\u5df1\u7684\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f current_app\uff1a<\/p>\n<p><strong>a.py<\/strong><\/p>\n<pre>from flask import flask\nfrom flask_sqlalchemy import sqlalchemy\n\napp = flask(__name__)\ndb = sqlalchemy()\n\nsetting = app.config[\"database\"]\napp.config[\"sqlalchemy_database_uri\"] = f'mysql+pymysql:\/\/{setting[\"db_user\"]}:{setting[\"db_pass\"]@{setting[\"db_host\"]}\/{setting[\"db_name\"]}'\n\ndb.init_app(app)<\/pre>\n<p><strong>b.py<\/strong><\/p>\n<pre>from model.user import User\nfrom a import DB, app\n\nclass Account:\n\n    @staticmethod\n    def login(username, password):\n        with app.test_context():\n            user = DB.session.execute(DB.select(User).filter_by(name=username)).scalar_one()\n\n        return \"token\"<\/pre>\n<p>\u901a\u8fc7\u5c06 app \u4f5c\u4e3a\u4e0a\u4e0b\u6587\u672c\u7ba1\u7406\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u6a21\u62df\u771f\u5b9e\u5e94\u7528\u7a0b\u5e8f\u7684\u4e0a\u4e0b\u6587\uff0c\u4ece\u800c\u6b63\u786e\u521d\u59cb\u5316\u6570\u636e\u5e93\u5e76\u4f7f\u7528 current \u5bf9\u8c61\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aFlask-SQLAlchemy \u5de5\u7a0b\u5316\u65f6\uff0c\u5982\u4f55\u907f\u514d\\&#8221;The setup method \\&#8217;shell_context_processor\\&#8217; can no longer be called on the application\\&#8221;\u9519\u8bef\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flask-SQLAlchemy&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205055","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205055"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205055\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}