如何基于SQLAlchemy模型创建Flask-Admin视图

问题描述 投票:0回答:0

我有 2 个 SQLAlchemy 模型:

class Topic(Base):
    __tablename__ = 'topics'

    id = mapped_column(Integer, primary_key=True)
    name = mapped_column(String(50), unique=True)

    **articles = relationship('Article', back_populates='topic')**


class Article(Base):
    __tablename__ = 'articles'

    id = mapped_column(Integer, primary_key=True)
    title = mapped_column(String(100))
    description = mapped_column(Text)
    views_count = mapped_column(Integer, default=0)
    image = mapped_column(String(200), nullable=True)
    created_at = mapped_column(TIMESTAMP, server_default=func.CURRENT_TIMESTAMP())
    text = mapped_column(Text)
    foreign_link = mapped_column(Text, nullable=True)

    topic_id = mapped_column(Integer, ForeignKey('topics.id'))

    **topic = relationship('Topic', back_populates='articles')**

与 back_populates 关系相关。 当我尝试创建一个特定的

ArticleView
来填充 Flask-Admin 面板上的
topic
字段时:

class ArticleView(ModelView):
    form_columns = [
        'title',
        'description',
        'views_count',
        'image',
        'created_at',
        'text',
        'foreign_link',
        'topic',
    ]

我遇到了这样的错误: ...wtforms\小部件

python flask flask-sqlalchemy flask-login flask-admin
© www.soinside.com 2019 - 2024. All rights reserved.