英文:
Django limit query not working in get_queryset
问题
I overwrite get_queryset as below:
def get_queryset(self, request):
qs = super().get_queryset(request)
qs.order_by('-id').all()[:3]
print(qs)
print()
qs2 = CreditsTransaction.objects.all()[:3]
print(qs2)
return qs
This is my output:
[2023-04-19 08:53:52 +0000] [1178] [INFO] Booting worker with pid: 1178
<QuerySet [<CreditsTransaction: CreditsTransaction object (623267)>, <CreditsTransaction: CreditsTransaction object (623266)>, <CreditsTransaction: CreditsTransaction object (623265)>, <CreditsTransaction: CreditsTransaction object (623264)>, <CreditsTransaction: CreditsTransaction object (623263)>, ...(remaining elements truncated)...]>
<QuerySet [<CreditsTransaction: CreditsTransaction object (623267)>, <CreditsTransaction: CreditsTransaction object (623266)>, <CreditsTransaction: CreditsTransaction object (623265)>]>
So in qs.order_by('-id').all()[:3]
the number of results is not limited to 3. Whereas in CreditsTransaction.objects.all()[:3]
it is. I want to know why.
英文:
I overwrite get_queryset as below:
def get_queryset(self, request):
qs = super().get_queryset(request)
qs.order_by('-id').all()[:3]
print(qs)
print()
qs2 = CreditsTransaction.objects.all()[:3]
print(qs2)
return qs
This is my output:
[2023-04-19 08:53:52 +0000] [1178] [INFO] Booting worker with pid: 1178
<QuerySet [<CreditsTransaction: CreditsTransaction object (623267)>, <CreditsTransaction: CreditsTransaction object (623266)>, <CreditsTransaction: CreditsTransaction object (623265)>, <CreditsTransaction: CreditsTransaction object (623264)>, <CreditsTransaction: CreditsTransaction object (623263)>, '...(remaining elements truncated)...']>
<QuerySet [<CreditsTransaction: CreditsTransaction object (623267)>, <CreditsTransaction: CreditsTransaction object (623266)>, <CreditsTransaction: CreditsTransaction object (623265)>]>
So in qs.order_by('-id').all()[:3]
the number of results is not limited to 3. Whereas in CreditsTransaction.objects.all()[:3]
it is. I want to know why.
答案1
得分: 1
你的代码中存在逻辑错误。qs.order_by('-id').all()[:3]
这一行没有保存回 qs
变量中。因此,你打印的是原始的 qs
。
你需要使用 qs = qs.order_by('-id').all()[:3]
来得到你想要的结果。
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.order_by('-id').all()[:3]
print(qs)
print()
qs2 = CreditsTransaction.objects.all()[:3]
print(qs2)
return qs
英文:
You have a logical error in the code. The line qs.order_by('-id').all()[:3]
does not get saved back in qs
variable. So, you print the original qs
.
You need to do qs = qs.order_by('-id').all()[:3]
in order to get what you want.
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.order_by('-id').all()[:3]
print(qs)
print()
qs2 = CreditsTransaction.objects.all()[:3]
print(qs2)
return qs
答案2
得分: 1
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.order_by('-id')[:3] # 将新的 QuerySet 赋值给 qs
print(qs)
print()
qs2 = CreditsTransaction.objects.all()[:3]
print(qs2)
return qs
你没有保存回查询集。
英文:
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.order_by('-id')[:3] # assign the new QuerySet to qs
print(qs)
print()
qs2 = CreditsTransaction.objects.all()[:3]
print(qs2)
return qs
you are not saving back the queryset
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论