Django-tables2 renders same first and last row

188 views Asked by At

I have a weird problem with Django-tables2. I've created a simple table with two columns - timestamp and description.

The problem is that the first object from QuerySet is not being rendered in the table. Instead of first object, there is last object copied in this row.

enter image description here

Now I'm populating this table with Action objects.

MODEL

class Action(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name='actions')
    description = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return u"{} - {} {}".format(self.user, self.timestamp, self.description)

VIEW

@decorators.is_authenticated_or_homepage
def dashboard(request):
    print Action.objects.filter(user=request.user)
    recent_actions_table = RecentActionsTable(Action.objects.filter(user=request.user))
    context = {'user': request.user,
               'recent_actions_table':recent_actions_table}
    return render(request, 'main_app/dashboard/index.html', context=context)

TABLE

class RecentActionsTable(tables.Table):

    class Meta:
        model = Action
        fields = ('id','timestamp','description')
        attrs = {'id': 'id_recent_actions_table',
                 'class': 'table', }

As you can see, I've printed the queryset before creating a table:

    <QuerySet [<Action: futilestudio - 2016-12-29 16:15:33.299000 New product created (6)>, 
<Action: futilestudio - 2016-12-29 16:53:29.534000 Manual scan of product>,
 <Action: futilestudio - 2016-12-29 17:05:38.215000 Manual scan of product>,
 <Action: futilestudio - 2016-12-29 17:27:05.462000 New product created (7)>]>

The first object is not in the table. Instead, there is duplicated object with ID 5.

Do you have any ideas what's wrong?

1

There are 1 answers

0
Milano On

So I found a solution for this problem but I don't know what had caused it.

I've just configured table to allow ordering:

@decorators.is_authenticated_or_homepage
def dashboard(request):
    user = request.user
    recent_actions_table = RecentActionsTable(Action.objects.filter(user=request.user))

    # >>>>>>>>>>>> ADDED THE LINE BELOW <<<<<<<<<<<<<
    RequestConfig(request).configure(recent_actions_table)

    context = {'user': user,
               'recent_actions_table':recent_actions_table}
    return render(request, 'main_app/dashboard/index.html', context=context)