문제

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.

다른 팁

For such numbers of items to put in table and to be able to sort the table without refreshing the whole page, I would suggest you to use SlickGrid which is a javascript library that takes json data as input.

Just dump your queryset in a json var and pass it to your template.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top