如何获取django查询集的下一行?

时间:2023-01-27 16:05:17

I have a queryset with that will return several rows in it. One of the row has a slug "slug-456", how do I get to the next row after that row having the slug "slug-456" with respect to the current ordering the queryset have?

我有一个查询集,将返回其中的几行。其中一行有一个slug“slug-456”,如果相对于查询集的当前排序具有slug“slug-456”的那一行之后,我如何到达下一行?

I can loop through the queryset and check if the current row has the slug "slug-456" and if it has take note that the next row is what I'm after, but Is there a more efficient way?

我可以遍历查询集并检查当前行是否有slug“slug-456”,如果它注意到下一行是我所追求的,但是有更有效的方法吗?

UPDATE: I did it this way:

更新:我这样做了:

id_list = list(qs.values_list('id', flat=True))
try:
    next_id = id_list[id_list.index(obj.id) + 1]
    obj = Object.objects.get(id=next_id)
except IndexError:
    pass

2 个解决方案

#1


5  

Querysets are generators so there isn't really a shortcut along the lines of qs[qs.indexof(slug="..")+1]. Any solution you come up with still require iterating through the queryset (at least till the target object).

查询集是生成器,因此qs [qs.indexof(slug =“..”)+ 1]的行中没有真正的快捷方式。您提出的任何解决方案仍然需要遍历查询集(至少直到目标对象)。

As you mention, a possible way to do it would be to loop through the queryset and return the one right after the one with slug="slug-456".

正如您所提到的,可能的方法是循环查询集并在slug =“slug-456”之后返回一个查询集。

You could of course take a more convoluted route and do something along the lines of:

你当然可以采取更复杂的路线,并采取以下措施:

# get slugs in order
sio = list(qs.objects.values_list('slug', flat=True))
target_slug = sio[sio.index(sio) + 1] # watch for KeyError
your_object = qs.objects.get(slug__exact=target_slug)

while amusing to write, this is unlikely to give any performance benefits (unless your model has many fields in which case iterating through the output of values_list() first might be beneficial).

虽然有趣的是写,但这不太可能带来任何性能上的好处(除非你的模型有很多字段,在这种情况下迭代values_list()的输出可能是有益的)。

Related answer: Get the index of an element in a queryset.

相关回答:获取查询集中元素的索引。

#2


6  

For py<2.6,

queryset.iterator().next()

For >= 2.6

对于> = 2.6

next(queryset.iterator())

should do the trick

应该做的伎俩

#1


5  

Querysets are generators so there isn't really a shortcut along the lines of qs[qs.indexof(slug="..")+1]. Any solution you come up with still require iterating through the queryset (at least till the target object).

查询集是生成器,因此qs [qs.indexof(slug =“..”)+ 1]的行中没有真正的快捷方式。您提出的任何解决方案仍然需要遍历查询集(至少直到目标对象)。

As you mention, a possible way to do it would be to loop through the queryset and return the one right after the one with slug="slug-456".

正如您所提到的,可能的方法是循环查询集并在slug =“slug-456”之后返回一个查询集。

You could of course take a more convoluted route and do something along the lines of:

你当然可以采取更复杂的路线,并采取以下措施:

# get slugs in order
sio = list(qs.objects.values_list('slug', flat=True))
target_slug = sio[sio.index(sio) + 1] # watch for KeyError
your_object = qs.objects.get(slug__exact=target_slug)

while amusing to write, this is unlikely to give any performance benefits (unless your model has many fields in which case iterating through the output of values_list() first might be beneficial).

虽然有趣的是写,但这不太可能带来任何性能上的好处(除非你的模型有很多字段,在这种情况下迭代values_list()的输出可能是有益的)。

Related answer: Get the index of an element in a queryset.

相关回答:获取查询集中元素的索引。

#2


6  

For py<2.6,

queryset.iterator().next()

For >= 2.6

对于> = 2.6

next(queryset.iterator())

should do the trick

应该做的伎俩