Django-tables2呈现相同的第一行和最后一行

时间:2016-12-29 16:40:06

标签: python django django-models django-tables2 django-1.10

Django-tables2我遇到了一个奇怪的问题。我创建了一个包含两列的简单表格 - timestampdescription

问题是来自QuerySet的第一个对象未​​在表中呈现。而不是第一个对象,在此行中复制了最后一个对象。

enter image description here

现在我用Action个对象填充此表。

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)

查看

@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)

表格

class RecentActionsTable(tables.Table):

    class Meta:
        model = Action
        fields = ('id','timestamp','description')
        attrs = {'id': 'id_recent_actions_table',
                 'class': '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)>]>

第一个对象不在表中。相反,存在ID为5的重复对象。

你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

所以我找到了解决这个问题的方法,但我不知道是什么导致了这个问题。

我刚刚配置了表以允许订购:

@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)