Django,使用"|":表达式树太大(最大深度为1000) [英] Django, using "|": Expression tree is too large (maximum depth 1000)
问题描述
我正在尝试将许多查询集连接在一起.我从这个提了一段时间的问题,但这在我的情况下不起作用.我需要返回一个 queryset
而不是一个列表.因此,我使用了第二个答案中的 |
.当时效果不错,但是现在我尝试将其再次用于其他用途,但出现以下错误:
表达式树太大(最大深度为1000)
我本来以为 |
会合并查询集,但是在阅读文档后,它似乎会合并实际的查询.如果查询变得太长/太复杂,就会发生此特定问题.
这就是我想要做的:
def属性(自身,请求,pk =无):项目= self.get_object()如果项目为None:return Response({'detail':'Missing project id'},status = 404)功能= Function.objects.filter(project = project)属性= Property.objects.none()对于功能中的功能:属性=属性|function.property_set.all()返回响应([属性中x的PropertySerializer(x).data])
由于 function
查询返回大约1200个结果,并且每个 function
具有大约5个属性,因此我可以理解查询太长/太复杂了.
如何防止查询变得过于复杂?或者我该如何执行多个查询并在以后合并它们,同时将最终结果保留为一个查询集?
我想您想获取所有具有 Function
某个项目的 Property
对象./p>
我们可以通过以下方式对此进行查询:
properties = Property.objects.filter(function__project = project)
因此,这是一个查询集,其中包含 function
(我假设这是 ForeignKey
)具有作为 project
的所有属性对象(可能又是 ForeignKey
是给定的 project
).这也将导致单个查询,但是您将避免构造巨大的联合.
或者,您可以分两步执行此操作,但这实际上会使它变慢:
#效率可能较低function_ids =(Function.objects.filter(project = project).values_list('pk',flat = True))properties = Properties.object(function_id__in = function_ids)
I'm trying to concatenate many querysets together. I tried out the marked answer from this question a while back, but that didn't work in my case. I needed to return a queryset
not a list. So I used the |
, from the second answer. This worked fine at the time, but now that I'm trying to use it again for something else I get the following error:
Expression tree is too large (maximum depth 1000)
I originally thought that |
would concat the querysets, but after reading the docs it appears that it concats the actual query. And that this specific problem occurs if the query becomes too long/complex.
This is what I'm trying to do:
def properties(self, request, pk=None):
project = self.get_object()
if project is None:
return Response({'detail': 'Missing project id'}, status=404)
functions = Function.objects.filter(project=project)
properties = Property.objects.none()
for function in functions:
properties = properties | function.property_set.all()
return Response([PropertySerializer(x).data for x in properties])
Since the functions
query returns roughly 1200 results, and each function
has about 5 properties, I can understand the query becoming too long/complex.
How can I prevent the query from becoming too complex? Or how can I execute multiple queries and concat them afterwards, while keeping the end result a queryset?
I think you want to obtain all the Property
objects that have as Function
a certain project.
We can query this with:
properties = Property.objects.filter(function__project=project)
This thus is a queryset that contains all property objects for which the function
(I assume this is a ForeignKey
) has as project
(probably again a ForeignKey
is the given project
). This will result in a single query as well, but you will avoid constructing gigantic unions.
Alternatively, you can do it in two steps, but this would actually make it slower:
# probably less efficient
function_ids = (Function.objects.filter(project=project)
.values_list('pk', flat=True))
properties = Properties.object(function_id__in=function_ids)
这篇关于Django,使用"|":表达式树太大(最大深度为1000)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!