вот пример с https://pypi.org/project/Flask-SQLAlchemy/
(A Simple Example)
По традиции там пример не рабочий, слегка доработал, теперь оно создает базу и пишет туда:
#A Simple Example from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite" app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String, unique=True, nullable=False) email = db.Column(db.String, unique=True, nullable=False) def __repr__(self): return '<User %r>' % self.username db.create_all() # добавил, в док. не было # ============================================ db.session.add(User(username="Flask", email="example@example.com")) db.session.add(User(username="Django", email="dj@example.com")) db.session.commit() users = User.query.all() for u in users: print(f"{u.username =}") print(f"{u =}")
Теперь этот, заведомо рабочий пример я хочу разбить на app.py и models.py - и не выходит. Прошу помочь. Перепробовал уже кучу вариантов.
app.py:
#A Simple Example from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) """ app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite" app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True """ SQLALCHEMY_DATABASE_URI = "sqlite:///example.sqlite" SQLALCHEMY_TRACK_MODIFICATIONS = True #db = SQLAlchemy(app) from models import db #from models import * #db.init_app(app) db.init_app() """ class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String, unique=True, nullable=False) email = db.Column(db.String, unique=True, nullable=False) def __repr__(self): return '<User %r>' % self.username """ db.create_all() # добавил, в док. не было # ============================================ db.session.add(User(username="Flask", email="example@example.com")) db.session.add(User(username="Django", email="dj@example.com")) db.session.commit() users = User.query.all() for u in users: print(f"{u.username =}") print(f"{u =}")
models.py:
from flask import Flask from flask_sqlalchemy import SQLAlchemy from app import * SQLALCHEMY_DATABASE_URI = "sqlite:///example.sqlite" SQLALCHEMY_TRACK_MODIFICATIONS = True db = SQLAlchemy(app) #db = SQLAlchemy() class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String, unique=True, nullable=False) email = db.Column(db.String, unique=True, nullable=False) def __repr__(self): return '<User %r>' % self.username
ошибка:
.... from models import db ImportError: cannot import name 'db' from partially initialized module 'models' (most likely due to a circular import) (W:\....\models.py)