datatables
datatables copied to clipboard
How to sort by joined data
If I have users and addresses and i'm creating a table of addresses, i want to also show user name and sort by it, not just by address.user_id. how can i do this?
table = DataTable(request.args, Address, db.session.query(Address).join(User),
[("address_line_1"),
("address_line_2"),
("zip",),
("user", "user_id", lambda i: i.name), <-- this shows name in the table, but sorts by id i think
("user", "user.name"), <-- this does not work at all
])
ok, i think i've figured this out. seems like the join to User is unnecessary and results in alias issues.
this seems to work, including properly sorting by user.name instead of user_id:
table = DataTable(request.args, Address, db.session.query(Address),
[("address_line_1"),
("address_line_2"),
("zip",),
("user", "user.name", lambda i: i.name)
])
it seems the filter for the user will get passed a User object.