Flask-AppBuilder: How to sort on relationship?

497 Views Asked by At

According to the documentation, using order_columns you can specify which columns allow sorting, which adds blue arrows in the header to select sorting in ascending or descending order.

However I also want to order by a relationship called "softwareproduct" to another table but when I add that to order_columns, it crashes (as it is not a real column but a relationship). The documentation also lists order_rel_fields, which I tried as well but that doesn't add a sorting function to the "softwareproduct" "column"/relationship: screenshot of the F.A.B. prototype where the "Softwareproduct" "column"/relationship is not sortable

Add_columns, edit_columns, show_columns and list_columns work perfectly fine, only order doesn't, even though "softwareproduct" isn't technically a real column but a relationship.

How can I let the users sort on such relationships?

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]
1

There are 1 best solutions below

0
On

Change

order_columns= ["label","suffix"]

To

base_order = ("label", "asc")