Django - limiting query results

后端 未结 5 933
说谎
说谎 2020-12-04 08:03

I want to take the last 10 instances of a model and have this code:

 Model.objects.all().order_by(\'-id\')[:10]

Is it true that firstly pic

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 08:50

    As an addition and observation to the other useful answers, it's worth noticing that actually doing [:10] as slicing will return the first 10 elements of the list, not the last 10...

    To get the last 10 you should do [-10:] instead (see here). This will help you avoid using order_by('-id') with the - to reverse the elements.

提交回复
热议问题