Hi,
I have two tables, link by a relationship :
class News(db.Model):
#[...]
venue_id = db.Column(db.Integer(), db.ForeignKey(Venue.id))
venue = db.relationship(Venue, backref='news')
When creating a News item, venue objects are displayed in a select, but there are not ordered, how could I do that ?
Thanks you !
All you have to do is to tell Flask-Admin which field to use for sorting. By default Flask-Admin won't make any assumptions about it.
This is snippet from sqla:
class PostAdmin(sqlamodel.ModelView):
# List of columns that can be sorted. For 'user' column, use User.username as
# a column.
column_sortable_list = ('title', ('user', User.username), 'date')
user
is relation to the User
table. If you wish, you can point it to User.id
too.
This seems to affect what is sortable in the list view. What about managing the sort order in the
@mistercrunch (and others who find this via Google like me), you could update the query_factory
property on the form field. Something like this:
def edit_form(self, obj=None):
form = super().edit_form(obj)
# apply a sort to the relation
form.venue.query_factory = lambda: models.Venue.query.order_by(models.Venue.name)
return form
Most helpful comment
All you have to do is to tell Flask-Admin which field to use for sorting. By default Flask-Admin won't make any assumptions about it.
This is snippet from sqla:
user
is relation to theUser
table. If you wish, you can point it toUser.id
too.