Форум сайта python.su
Добрый день!
вот пример с 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 =}")
#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 =}")
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)
Отредактировано Florenzo (Янв. 20, 2021 22:06:44)
Прикреплённый файлы:
flask_sqlalchemy_for_python.su.7z (737 байт)
Офлайн
У тебя обычный циклический импорт. Убери строчку from app import * из models.py и должно заработать.
Офлайн