如何在starlette-admin中显示相关项目?

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

我想使用 starlette-admin 显示相关项目,但我对此很陌生,不明白我应该更改哪一部分。 例如,我有

orm.py

from sqlalchemy.orm import relationship
from models.tables import Base, theme_table, post_table, comment_table


class Theme(Base):
    __table__ = theme_table


class Post(Base):
    __table__ = post_table

    theme = relationship(Theme)


class Comment(Base):
    __table__ = comment_table

    theme = relationship(Theme)

admin_panel.py

from sqlalchemy import create_engine
from starlette_admin.contrib.sqla import Admin, ModelView
from models.orm import Theme, Comment, Post

def init_admin(app):
    engine = create_engine("sqlite:///test.db", connect_args={"check_same_thread": False})
    admin = Admin(
        engine,
        base_url='/test/admin',
        title='Test Admin'
    )
    class ThemeAdmin(ModelView):
        name = 'Theme'
        label = 'Themes'

     class PostAdmin(ModelView):
        name = 'Post'
        label = 'Posts'
    
    class CommentAdmin(ModelView):
        name = 'Comment'
        label = 'Comments'

我想在详细主题页面上显示所有帖子、评论(带链接)。例如,如果我打开 id = 1 的主题,我想显示所有帖子,其中 theme_id = 1 等。我想保存 starlette-admin 样式,只需扩展模式即可。我想将其显示在表格上,就像通常的属性一样。

我将非常感谢任何建议!

python django-admin fastapi admin starlette
1个回答
0
投票

您需要在主表中添加主键,在子表中添加外键和关系。

class MainTable(Base):
    __table__ = "main_table"

    id = Column(Integer, primary_key=True)

class SubTable(Base):
    __table__ = "sub_table"

    id = Column(Integer, primary_key=True)
    main_id = Column(ForeignKey("main_table.id"))
    rel = relationship("MainTable")



class SubTableView(ModelView):
    fields = ["id", "rel"]
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.