如何创建Flask-AppBulider ModelView,该视图列出了两个表的联合输出?

我是flask-AppBulider和SqlAlchecmy的新手,并试图为运行flask-appbuilder的Apache Airflow编写UI插件。 这里有两个代表两个表的模型: SoundTrack (exp_track)Album (exp_album)。 SongsModelView是具有

的ModelView
datamodel = SQLAInterface(SoundTrack)

该代码段在三个不同的文件中包含三个类定义。

在数据库中创建表就很好了。代码段还包括来自数据库的表定义(此处为sqlite)。 SoundTrack模型中的列显示正常。 但是任何需要从外键中获取的列都显示为空。

经过一番阅读,我尝试在相册模型中使用lazy = relationship定义一个joined,但是exp_album.title列仍然为空。

exp_track.py

from airflow.models.base import Base
from airflow.utils.log.logging_mixin import LoggingMixin
from airflow import settings

from sqlalchemy import Column,Integer,String,ForeignKey

class SoundTrack(Base,LoggingMixin):
    __tablename__ = "exp_track"

    id = Column(Integer,primary_key=True,autoincrement=True)
    title = Column(String(256),nullable=False)
    album_id = Column(Integer,ForeignKey("exp_album.id"),nullable=False)

    __table_args__ = (
        {'extend_existing': True}
    )


SoundTrack.__table__.create(settings.engine,checkfirst=True)

exp_album.py

from airflow.models.base import Base
from airflow.utils.log.logging_mixin import LoggingMixin
from airflow import settings

from sqlalchemy import Column,String
from sqlalchemy.orm import relationship

class Album(Base,LoggingMixin):
    __tablename__ = "exp_album"

    id = Column(Integer,nullable=False)
    artist = Column(String(256),nullable=False)

    tracks = relationship("my_songs_plugin.model.exp_track.SoundTrack",backref='track',lazy="joined")

    __table_args__ = (
        {'extend_existing': True}
    )


Album.__table__.create(settings.engine,checkfirst=True)

exp_song_view.py

from flask_appbuilder import ModelView
from flask_appbuilder.models.sqla.interface import SQLAInterface

from airflow.www_rbac.widgets import AirflowModelListWidget

from my_songs_plugin.model.exp_track import SoundTrack


class SongsModelView(ModelView):
    list_widget = AirflowModelListWidget
    page_size = 100

    route_base = '/songs'

    datamodel = SQLAInterface(SoundTrack)

    base_permissions = ['can_list']

    list_columns = ['id','title','album_id','exp_album.title']


songs_view = SongsModelView()
songs_view_data = {"category": "My Songs Plugin","name": "Songs","view": songs_view}

表定义:

CREATE TABLE exp_track (
    id INTEGER NOT NULL,title VARCHAR(256) NOT NULL,album_id INTEGER NOT NULL,PRIMARY KEY (id),FOREIGN KEY(album_id) REFERENCES exp_album (id)
);


CREATE TABLE exp_album (
    id INTEGER NOT NULL,artist VARCHAR(256) NOT NULL,PRIMARY KEY (id)
);

我不知道如何显示加入的列表。

tuxiangzhen 回答:如何创建Flask-AppBulider ModelView,该视图列出了两个表的联合输出?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3125374.html

大家都在问