Get the index of an element in a queryset

后端 未结 6 810
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 18:08

I have a QuerySet, let\'s call it qs, which is ordered by some attribute which is irrelevant to this problem. Then I have an object, let\'s call it obj

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 18:27

    Compact and probably the most efficient:

    for index, item in enumerate(your_queryset):
        ...
    

提交回复
热议问题