I've trying to mess around with django-tables2 for creating a leaderboard table that allows users to sort, search, and filter by all columns. I'm not doing anything special and just followed the documentation from django-tables2.
Here's what it looks like in the model:
class LeaderboardTable(tables.Table):
rank = tables.TemplateColumn('{{ record.rank }}')
name = tables.TemplateColumn('{{ record.user__first_name }} {{ record.user__last_name }}')
team = tables.TemplateColumn('{{ record.team }}')
points = tables.TemplateColumn('{{ record.points }}')
Here's what it looks like in the view:
def get_leaderboard(request):
table = LeaderboardTable(Profiles.objects.select_related().all())
RequestConfig(request).configure(table)
return render(request, 'leaderboard.html', {'table': table})
If you look at the above code you'll probably notice that the name column isn't correct, since I haven't figured how to sort if I combine to fields from a queryset - if you can answer this also that would be great.
I like that django-tables2 handles creation of the table and pagination; however, every time I hit sort/next or prev page it causes a page refresh. Is there a way to suppress this? Also, I dont know if its because I'm returning 10,000 records or not, but django-tables2 seems pretty slow on sorting and paging.
Am I using the correct app for this? Do you guys think I should use something else?
django-tables2 does not support using JavaScript to do sorting/pagination without refreshing the page (at this time).
There shouldn't be an issue with returning 10k records. I suggest you use something like django-debug-toolbar to have a look at which queries are slow to execute (e.g. perhaps your select_related()
call isn't working).
You can rewrite your table as:
class LeaderboardTable(tables.Table):
rank = tables.Column()
name = tables.Column(order_by=("first_name", "last_name"))
team = tables.Column()
points = tables.Column()
def render_name(self, record):
return u"%s %s" % (record.user.first_name, record.user.last_name)
Check if using render_name
resolves your performance issue. It should be faster than using TemplateColumn
, but that should only be significant if you're rendering a huge number of rows in each page of the table.