简体   繁体   English

Flask Admin 和 SQLAlchemy 关系建议,如何从祖父表中获取列?

[英]Flask Admin and SQLAlchemy relationship advice, how do I get columns from a grandparent table?

Using Flask Admin, I'm having difficulties understanding how to retrieve columns from parent and grandparent tables to use for filtering / create dropdowns使用 Flask Admin,我很难理解如何从父表和祖父表中检索列以用于过滤/创建下拉列表

class Project(db.Model):

    project_id=db.Column(db.Integer, primary_key=True)
    title=db.Column(db.String(100), nullable=False)

    def __repr__(self):
        return self.title

    def __str__(self):
       return self.title

    def get_id(self):
        return self.project_id

class Story(db.Model):

    story_id=db.Column(db.Integer, primary_key=True)
    description=db.Column(db.String(), nullable=False)

    project_id=db.Column(db.Integer, db.ForeignKey(Project.project_id))
    project=db.relationship(Project)

    def __repr__(self):
        return self.description

    def __str__(self):
       return self.description

    def get_id(self):
        return self.story_id

class Task(db.Model):

    task_id=db.Column(db.Integer, primary_key=True)
    description=db.Column(db.String(), nullable=False)
    story_id=db.Column(db.Integer, db.ForeignKey(Story.story_id))
    story=db.relationship(Story)

    def __repr__(self):
        return self.description

    def __str__(self):
       return self.description

    def get_id(self):
        return self.task_id

class TaskModelView(ModelView):

    create_modal = False
    edit_modal = False
    can_set_page_size = True
    page_size = 20
    column_display_pk = True
    column_display_all_relations = True

admin.add_view(TaskModelView(Task, db.session))

When dealing with the Tasks list, I see the Story description with no problems and can use that to filter the list but how would I obtain the Project title and be able to filter on that column?在处理任务列表时,我看到故事描述没有问题,可以使用它来过滤列表,但我如何获得项目标题并能够过滤该列?

Have been searching the docs but obviously missing something ..一直在搜索文档,但显然遗漏了一些东西..

Maybe this can help.也许这会有所帮助。

class TaskModelView(ModelView):
    ...
    column_list = [ 'description', 'story', ]
    column_filters = ['description', 'story.description', ]
    column_labels = {'story.description': 'Story Desc.'}

References here .参考这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM