Ошибка утверждения с flask и SQL Алхимия - PullRequest
0 голосов
/ 05 августа 2020

У меня следующая flask структура:

введите описание изображения здесь

Со следующим кодом:

init.py

from press_app.views import app
from press_app import models

# Connect sqlalchemy to app
models.db.init_app(app)

@app.cli.command()
def init_db():
    models.init_db()

models.py

from flask_sqlalchemy import SQLAlchemy
import logging as lg
from press_app.views import app

# Create database connection object
db = SQLAlchemy(app)

class Content(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author = db.Column(db.String(100), nullable=True)
    content = db.Column(db.String(700), nullable=True)
    description = db.Column(db.String(700), nullable=True)
    publishedAt = db.Column(db.DateTime())
    source = db.Column(db.String(100), nullable=True)
    title = db.Column(db.String(200), nullable=True)
    url = db.Column(db.String(1000), nullable=True)
    urlToImage = db.Column(db.String(1000), nullable=True)

    def __init__(self, author, content, description, publishedAt, source, title, url, urlToImage):
        self.author = author
        self.content = content
        self.description = description
        self.publishedAt = publishedAt
        self.source = source
        self.title = title
        self.url = url
        self.urlToImage = urlToImage

db.create_all()

def init_db():
    db.drop_all()
    db.create_all()
    db.session.commit()
    lg.warning('Database initialized!')

views.py

from flask import Flask, render_template
from press_app.models import Content

app = Flask(__name__)

# Config options - Make sure you created a 'config.py' file.
app.config.from_object('config')
# To get one variable, tape app.config['MY_VARIABLE']

@app.route('/')
def index():
    last_article = Content.query.order_by(Content.id.desc()).first()
    last_id = last_article.id

    # Derniers titres
    title1 = Content.query.get(last_id).title
    title2 = Content.query.get(last_id - 1).title

    return render_template("index3.html", titre1 = title1)

if __name__ == "__main__":
    app.run()

config.py

import os

basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')

и run.py

from press_app import app
if __name__ == "__main__":
    app.run()

Из-за следующего сообщения об ошибке:

ImportError: cannot import name 'app' from partially initialized module 'press_app.views' (most likely due to a circular import) 

Я решил переместить один из моих импортов в файл views.py непосредственно в моя функция, как показано ниже:

views.py, вторая версия

from flask import Flask, render_template

app = Flask(__name__)

# Config options - Make sure you created a 'config.py' file.
app.config.from_object('config')
# To get one variable, tape app.config['MY_VARIABLE']

@app.route('/')
def index():
    from press_app.models import Content

    last_article = Content.query.order_by(Content.id.desc()).first()
    last_id = last_article.id

    # Derniers titres
    title1 = Content.query.get(last_id).title
    title2 = Content.query.get(last_id - 1).title

    return render_template("index3.html", titre1 = title1)

if __name__ == "__main__":
    app.run()

Однако я столкнулся с новым сообщением об ошибке (ошибка 500), я могу ' t разрешается, несмотря на то, что я прочитал много документации и сообщений о переполнении стека по этому поводу.

AssertionError: The sqlalchemy extension was not registered to the current application.  Please make sure to call init_app() first.

Как я могу решить эту ситуацию?

1 Ответ

1 голос
/ 05 августа 2020

Эта проблема связана с тем, что вы инициализируете модели в другом файле, а flask приложение - в другом, ваше приложение необходимо инициализировать перед объектом sqlalchemy, чтобы вы могли его использовать.

Итак, ваши файлы должен выглядеть так.

views.py

from flask import Flask, render_template
from models import db

app = Flask(__name__)

# Config options - Make sure you created a 'config.py' file.
app.config.from_object('config')
# To get one variable, tape app.config['MY_VARIABLE']
db.init_app(app)
with app.app_context():
    db.create_all()
    db.session.commit()


@app.route('/')
def index():
    from models import Content

    last_article = Content.query.order_by(Content.id.desc()).first()
    last_id = last_article.id

    # Derniers titres
    title1 = Content.query.get(last_id).title
    title2 = Content.query.get(last_id - 1).title

    return render_template("index3.html", titre1 = title1)


if __name__ == "__main__":
    app.run()

models.py

from flask_sqlalchemy import SQLAlchemy
import logging as lg

# Create database connection object
db = SQLAlchemy()

class Content(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author = db.Column(db.String(100), nullable=True)
    content = db.Column(db.String(700), nullable=True)
    description = db.Column(db.String(700), nullable=True)
    publishedAt = db.Column(db.DateTime())
    source = db.Column(db.String(100), nullable=True)
    title = db.Column(db.String(200), nullable=True)
    url = db.Column(db.String(1000), nullable=True)
    urlToImage = db.Column(db.String(1000), nullable=True)

    def __init__(self, author, content, description, publishedAt, source, title, url, urlToImage):
        self.author = author
        self.content = content
        self.description = description
        self.publishedAt = publishedAt
        self.source = source
        self.title = title
        self.url = url
        self.urlToImage = urlToImage


def init_db():
    db.drop_all()
    db.create_all()
    db.session.commit()
    lg.warning('Database initialized!')
...