Проблема, с которой я столкнулся при использовании правильной структуры проекта, заключается в том, что я не знаю, как вызвать «приложение» при инициализации базы данных Strong>... Всегда выдает ошибку "
Код: Выделить всё
TypeError: cannot create weak reference to 'LocalProxy' object
Код: Выделить всё
db = SQLAlchemy(server)
Код: Выделить всё
server = Flask(__name__, instance_relative_config=False)
Я хочу начать с того, как выглядит структура моего проекта:
Код: Выделить всё
Proper Flask app (Parent folder)
l - Application (Folder)
l l
l l - dashboard (Folder)
l l l - dashboard.py
l l
l l - static (Folder)
l l l - assets (Folder)
l l l - css (Folder)
l l l l - styles.css
l l l - db (Folder)
l l l l - (This is where the db file will be created and resides.)
l l l - schema (Folder)
l l l - templates (Folder)
l l
l l - __init__.py (Initialization code of the Flask app.)
l l - models.py (This is where the code "class Finance_Table(db.Model)" resides.)
l l - routes.py (This is where the code for the routes resides.)
l l - utilities.py (This is where the other functions that I will need resides.)
l l
l - backups (Folder)
l - config.py (This is where the configuration settings for the flask resides)
l - main.py (This will run the __init__.py and start the Flask app.)
Код для "_init_.py":
Код: Выделить всё
from flask import Flask
def init_app():
server = Flask(__name__, instance_relative_config=False)
server.config.from_object("config.Config")
with server.app_context():
# Import core parts of Flask app:
from . import routes
# Import Dash Application: Example:
# from .dashboard.dashboard import init_dashboard
# server = init_dashboard(server)
"""
For this one, I am still unsure if its "server = " or
"app = "... Will know once I am doing this for real.
"""
return server
Код: Выделить всё
from flask import current_app as server
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(server)
class Finance_Table(db.Model):
transact_id = db.Column(db.Integer, primary_key=True)
transact_type = db.Column(db.String(10), nullable=False)
transact_sub_type = db.Column(db.String(100), nullable=False)
transact_date = db.Column(db.DateTime)
money = db.Column(db.Numeric, nullable=False)
note = db.Column(db.String(200), nullable=True)
def __repr__(self) -> str:
return f"
Подробнее здесь: [url]https://stackoverflow.com/questions/78466962/flask-sqlalchemy-how-to-properly-call-the-app-to-initialize-sqlalchemy-databa[/url]