繁体   English   中英

如何为特定列的唯一值过滤Django Tables2结果

[英]How to filter django tables2 results for unique values of a specific column

我有一个django table2表工作得很好,它仅显示具有正确项目ID的查询集。 我需要它来排除具有重复的user_id的查询集。

例:

用户a,第1周,项目1

用户a,第2周,项目1

用户b,第1周,项目1

显示分配给项目1的用户。(需要的结果是带有a和b行的表)

到目前为止,我有:

#models.py
class Allocation(models.Model):
    user_id = models.ForeignKey(Resource)
    project_id = models.ForeignKey(Project)
    week = models.DateField(default=timezone.now)
    allocated_hours = models.IntegerField(default=0)
    actual_hours = models.IntegerField(default=0)

#tables.py
class Project_Resource_table(tables.Table):
    role = tables.Column(accessor='user_id.resource_type')
    name = tables.Column(accessor='user_id.resource_name')
    allocation = tables.Column(accessor='total_allocation')

class Meta:
    model = Allocation
    exclude = ('id', 'user_id', 'project_id', 'week', 'allocated_hours', 'actual_hours')
    sequence = ('role', 'name', 'allocation')


#view (project_profile.py)
def project_properties(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    project = Project.objects.get(pk=offset)
    table = Project_Resource_table(Allocation.objects.filter(project_id=offset).order_by('user_id'))



return render(request, '../templates/copacity/project_profile.html', {
    'table': table,
    })

我试过使用set()但无济于事。 也尝试了distinct(),但不适用于sqlite3 db。 还尝试使用函数进行计算,但始终获取“期望的表或查询集,而不是函数”。 还有其他建议可以尝试吗?

您需要确保在创建表时传递QuerySet或字典列表。 如果django-tables2抱怨要获取一个函数,则是给它一个函数,而不是该函数返回的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM