Django限制查询在get_queryset中不起作用。

huangapple go评论48阅读模式
英文:

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(&#39;-id&#39;).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
&lt;QuerySet [&lt;CreditsTransaction: CreditsTransaction object (623267)&gt;, &lt;CreditsTransaction: CreditsTransaction object (623266)&gt;, &lt;CreditsTransaction: CreditsTransaction object (623265)&gt;, &lt;CreditsTransaction: CreditsTransaction object (623264)&gt;, &lt;CreditsTransaction: CreditsTransaction object (623263)&gt;, &#39;...(remaining elements truncated)...&#39;]&gt;

&lt;QuerySet [&lt;CreditsTransaction: CreditsTransaction object (623267)&gt;, &lt;CreditsTransaction: CreditsTransaction object (623266)&gt;, &lt;CreditsTransaction: CreditsTransaction object (623265)&gt;]&gt;

So in qs.order_by(&#39;-id&#39;).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(&#39;-id&#39;).all()[:3] 这一行没有保存回 qs 变量中。因此,你打印的是原始的 qs

你需要使用 qs = qs.order_by(&#39;-id&#39;).all()[:3] 来得到你想要的结果。

def get_queryset(self, request):
    qs = super().get_queryset(request)
    qs = qs.order_by(&#39;-id&#39;).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(&#39;-id&#39;).all()[:3] does not get saved back in qs variable. So, you print the original qs.

You need to do qs = qs.order_by(&#39;-id&#39;).all()[:3] in order to get what you want.

def get_queryset(self, request):
    qs = super().get_queryset(request)
    qs = qs.order_by(&#39;-id&#39;).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(&#39;-id&#39;)[: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

huangapple
  • 本文由 发表于 2023年4月19日 17:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定