Django group object by the first character of a column

前端 未结 3 718
甜味超标
甜味超标 2021-02-15 15:42

I\'m trying to group records by the character of name field of each record and limit the items in each group, here is what I have came up with:

desi         


        
3条回答
  •  孤街浪徒
    2021-02-15 16:32

    first ordering and then filtering is overkill and in vain. you should only order the data you need. Otherwise you are ordering all rows by name and then filtering and slicing what you need.

    I would do:

    User.objects.filter(name__startswith=desired_letter).order_by("name")[:10]
    

    and .all() was redundant.

提交回复
热议问题