Search code examples
djangodjango-tables2

Django Tables2 duplicated results


Django tables2 seems to be showing duplicated results for the top row and the last row.

E.g.

Database records shows:

ID|Name|User    
1|test1|1
2|test2|1

However table shown on the website is:

ID|Name|User
2|test2|1
1|test1|1
2|test2|1

Not sure why this is happening. Could you please help?

views.py

class ReportsView(ListView):
     model = Reports
     template_name = 'reports.html'

     def get_context_data(self, **kwargs):
         context = super(ReportsView, self).get_context_data(**kwargs)
         login_user = self.request.user
         context['reports_table'] = ReportsTable(Reports.objects.filter(user=login_user))
         return context

models.py

class Reports(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=30)
    description = models.CharField(max_length=50)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('edit_report', kwargs={'pk': self.pk})

tables.py

class ReportsTable(tables.Table):
    class Meta:
        model = Reports
        attrs = {'class': 'table table-bordered'}

reports.html

{% load render_table from django_tables2 %}
{% render_table reports_table %}

Solution

  • Seems like you not the only one faced with such problem. Try to set orderable = False:

    class ReportsTable(tables.Table):
        class Meta:
            model = Reports
            attrs = {'class': 'table table-bordered'}
            orderable = False