Get the index of an element in a queryset

后端 未结 6 826
佛祖请我去吃肉
佛祖请我去吃肉 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:31

    It's possible for a simple pythonic way to query the index of an element in a queryset:

    (*qs,).index(instance)
    

    This answer will unpack the queryset into a list, then use the inbuilt Python index function to determine it's position.

提交回复
热议问题