如何计算django模板中字典中不同键的数量
How to Count the number of distinct keys in a dictionary in django template
编号为序号,属于该序号的字典为数据。我想计算字典的长度属于 django 模板中的不同序列号。
词典名称为redict。
{15: [<videoscomment: Reply...byNavneetKaur>, <videoscomment: Reply...byNavneetKaur>], 20: [<videoscomment: 2nd comment reply...byNavneetKaur>]}
我试过以下方法
comments.sno是号码,下面获取属于该号码的数据
redict|get_val:comments.sno
我把计数放在最后一个来得到数据的长度。
redict|get_val:comments.sno.count
记住我们必须在 django 模板中这样做。
1 如果你熟悉注解:
querysetname = Model.objects.all().annotate(Count('The_Distinct_Field_REQD',
distinct=True))
或者创建一组查询集将只给出不同的元素,因为集合不允许重复值,
queryset = already_created_queryset
distinct_count = set(queryset).count() //add this line alone
或
distinct_count = Model.objects.all(filter as per your condition).distinct('field1',...'n_fields').count()
2 通过上下文字典传递计算出的非重复计数:
context = {
'dist_count' : distinct_count
}
3 使用模板中的dist_count
编号为序号,属于该序号的字典为数据。我想计算字典的长度属于 django 模板中的不同序列号。 词典名称为redict。
{15: [<videoscomment: Reply...byNavneetKaur>, <videoscomment: Reply...byNavneetKaur>], 20: [<videoscomment: 2nd comment reply...byNavneetKaur>]}
我试过以下方法
comments.sno是号码,下面获取属于该号码的数据
redict|get_val:comments.sno
我把计数放在最后一个来得到数据的长度。
redict|get_val:comments.sno.count
记住我们必须在 django 模板中这样做。
1 如果你熟悉注解:
querysetname = Model.objects.all().annotate(Count('The_Distinct_Field_REQD',
distinct=True))
或者创建一组查询集将只给出不同的元素,因为集合不允许重复值,
queryset = already_created_queryset
distinct_count = set(queryset).count() //add this line alone
或
distinct_count = Model.objects.all(filter as per your condition).distinct('field1',...'n_fields').count()
2 通过上下文字典传递计算出的非重复计数:
context = {
'dist_count' : distinct_count
}
3 使用模板中的dist_count