質問編集履歴

3

修正

2023/02/15 08:19

投稿

MagMag
MagMag

スコア80

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
  ### 困っていること・状況
7
7
  - Blueprintで分割した時、DBテーブル設定におけるコンフィグを読めない
8
8
  - 実行結果より、current_appがapplication context外で読まれていることが原因であることはわかるが、具体的にうまく読ませる方法がわかりません。
9
- - 例えば、7〜10行目をwith application.app_context()で囲むことも行ったが、「AttributeError: As of v1.0 PynamoDB Models must have a table_name」とエラーが返ってくる(クラス変数を適切に取得できていない?)
9
+ - 例えば、class Metaの内部をwith application.app_context()で囲むことも行ったが、「AttributeError: As of v1.0 PynamoDB Models must have a table_name」とエラーが返ってくる(クラス変数を適切に取得できていない?)
10
10
  - 関数(obtain_data)の中でclassを定義すると問題なく読み込むが、コードが重複するので避けたい
11
11
 
12
12
  ### 質問

2

順番修正

2023/02/15 05:46

投稿

MagMag
MagMag

スコア80

test CHANGED
File without changes
test CHANGED
@@ -18,6 +18,8 @@
18
18
  from pynamodb.models import Model
19
19
  from pynamodb.attributes import UnicodeAttribute
20
20
 
21
+ application = Flask(__name__)
22
+
21
23
  class DB(Model):
22
24
  class Meta:
23
25
  table_name = application.config.get('TABLE')
@@ -27,8 +29,6 @@
27
29
 
28
30
  partition_key = UnicodeAttribute(hash_key=True, null=False)
29
31
  data = UnicodeAttribute(null=True)
30
-
31
- application = Flask(__name__)
32
32
 
33
33
  @application.route('/obtain_dada/<partition_key>')
34
34
  def obtain_data(partition_key):
@@ -43,6 +43,10 @@
43
43
  from pynamodb.models import Model
44
44
  from pynamodb.attributes import UnicodeAttribute
45
45
 
46
+ application = Flask(__name__)
47
+ sub = Blueprint('sub', __name__)
48
+ application.register_blueprint(sub)
49
+
46
50
  class DB(Model):
47
51
  class Meta:
48
52
  table_name = current_app.config.get('TABLE')
@@ -52,11 +56,6 @@
52
56
 
53
57
  partition_key = UnicodeAttribute(hash_key=True, null=False)
54
58
  data = UnicodeAttribute(null=True)
55
-
56
-
57
- sub = Blueprint('sub', __name__)
58
- application = Flask(__name__)
59
- application.register_blueprint(sub)
60
59
 
61
60
  @sub.route('/obtain_dada/<partition_key>')
62
61
  def obtain_data(partition_key):

1

情報追加

2023/02/15 03:45

投稿

MagMag
MagMag

スコア80

test CHANGED
File without changes
test CHANGED
@@ -55,6 +55,9 @@
55
55
 
56
56
 
57
57
  sub = Blueprint('sub', __name__)
58
+ application = Flask(__name__)
59
+ application.register_blueprint(sub)
60
+
58
61
  @sub.route('/obtain_dada/<partition_key>')
59
62
  def obtain_data(partition_key):
60
63
  entries = DB.scan(partition_key)
@@ -64,6 +67,7 @@
64
67
 
65
68
 
66
69
  ### 実行結果
70
+ flask runして/obtain_dataにアクセスした結果
67
- This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve
71
+ "This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve
68
- this, set up an application context with app.app_context(). See the documentation for more information.
72
+ this, set up an application context with app.app_context(). See the documentation for more information."
69
73