“一旦获取切片,就不能过滤查询”。 [英] "Cannot filter a query once a slice has been taken"

查看:67
本文介绍了“一旦获取切片,就不能过滤查询”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误


渲染时捕获AssertionError:获取切片后无法过滤查询。

Caught AssertionError while rendering: Cannot filter a query once a slice has been taken.

在此行

{% if form.non_field_errors %} 

当我尝试执行此操作

copy_pickup_address = ModelChoiceField(required=False, queryset=Address.objects.filter(shipment_pickup__user=user).order_by('-shipment_pickup__created')[:5])

但是我需要对它进行切片,因为我只想要最后5个地址。效果很好,直到我选择一个地址并提交表格。为什么不这样呢?我该如何解决?

But I need to slice it, because I only want the last 5 addresses. It renders fine, until I choose an address and submit the form. Why doesn't it like this? How can I get around it?

推荐答案

没有人回答为什么不这样?您的问题的一部分。在 https://docs.djangoproject.com/en/1.8中给出了说明。 / ref / models / querysets /

No-one has yet answered the "Why doesn't it like this?" part of your question. The explanation is given at https://docs.djangoproject.com/en/1.8/ref/models/querysets/:


即使切片未评估的QuerySet也会返回另一个未评估的QuerySet,并对其进行进一步修改(例如,添加更多过滤器或修改顺序)是不允许的,因为这不能很好地转换为SQL,也没有明确的含义。

even though slicing an unevaluated QuerySet returns another unevaluated QuerySet, modifying it further (e.g., adding more filters, or modifying ordering) is not allowed, since that does not translate well into SQL and it would not have a clear meaning either.

这篇关于“一旦获取切片,就不能过滤查询”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆