django - show the length of a queryset in a template

前端 未结 4 972
走了就别回头了
走了就别回头了 2020-12-15 16:02

In my html file, how can I output the size of the queryset that I am using (for my debugging purposes)

I\'ve tried

{{ len(some_queryset) }}
         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 16:38

    Give {{ some_queryset.count }} a try.

    This is better than using len (which could be invoked with {{ some_queryset.__len__ }}) because it optimizes the SQL generated in the background to only retrieve the number of records instead of the records themselves.

提交回复
热议问题