FlaskAppBuilder:如何根据关系排序?

2024-06-08 18:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

根据documentation,使用order_columns可以指定哪些列允许排序,这会在标题中添加蓝色箭头以选择升序或降序排序

然而,我也希望通过一个名为“softwareproduct”的关系对另一个表进行排序,但当我将其添加到order_列时,它崩溃了(因为它不是一个真正的列,而是一个关系)。该文档还列出了order_rel_字段,我也尝试过了,但没有为“softwareproduct”列/关系添加排序功能: screenshot of the F.A.B. prototype where the "Softwareproduct" "column"/relationship is not sortable

添加列、编辑列、显示列和列表列都能很好地工作,只有顺序不能,即使“softwareproduct”从技术上讲不是一个真正的列,而是一种关系

我如何让用户根据这些关系进行排序

models.py

[...]
class Softwareproduct(Model):
    suffix = Column(String(200), primary_key=True)
    label =  Column(String(200), nullable=False)
    [...]
    def __repr__(self):
       return self.label

class Citation(Model):
    suffix = Column(String(200), primary_key=True)
    swp_suffix = Column(String(200), ForeignKey("softwareproduct.suffix"),nullable=False)
    softwareproduct = relationship("Softwareproduct")
    label =  Column(String(200), nullable=False)
                                                                                                                                                                                                                                                                                                                              
    def __repr__(self):
        return self.label

views.py

class CitationView(ModelView):
    datamodel = SQLAInterface(Citation)
    label_columns = {'label':'Citation', 'suffix': 'ID'}
    add_columns = ['softwareproduct', "label", "suffix", "classified"]
    edit_columns = ['softwareproduct', "label", "suffix","classified"]
    show_columns = ['softwareproduct', "label", "suffix","classified"]
    list_columns = ['softwareproduct', "label", "suffix","classified"]                                                                                                                                                                                                                                                        
    order_columns= ["label","suffix"]
    order_rel_fields = {'softwareproduct': ('label', 'asc')}
    related_views = [ClassifiedView]

Tags: columnsselffalsestring排序关系ordercolumn