我认为这是一个简单而直接的问题,我有两个查询集,我将它们合并为一个。 但是发生了这个错误。
When merging querysets using 'or', you cannot have extra(select=...) on both sides.
projects = pending_or_onhold | pending_by_manager
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\auth\decorators.py", line 25, in _wrapped_view
return view_func(request, *args, **kwargs)
File "D:/Pythonapps/ifscoatings\ifs_request_system\decorators.py", line 20, in inner
return view(request, *args, **kwargs)
File "D:/Pythonapps/ifscoatings\request_system\views.py", line 493, in dashboard
return render_to_response('dashboard.html', context_instance=RequestContext(request, ctx))
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py", line 29, in render_to_response
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "C:\Python27\lib\site-packages\django\template\loader.py", line 177, in render_to_string
return t.render(context_instance)
File "C:\Python27\lib\site-packages\django\template\base.py", line 140, in render
return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 134, in _render
return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 830, in render
bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 124, in render
return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 134, in _render
return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 830, in render
bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 63, in render
result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 830, in render
bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 483, in render
output = self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 830, in render
bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 284, in render
return nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 830, in render
bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 156, in render
return self.render_template(self.template, context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py", line 138, in render_template
output = template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 140, in render
return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 134, in _render
return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 830, in render
bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 483, in render
output = self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 830, in render
bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py", line 74, in render_node
return node.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 481, in render
six.iteritems(self.extra_context)])
File "C:\Python27\lib\site-packages\django\template\base.py", line 606, in resolve
new_obj = func(obj, *arg_vals)
File "D:/Pythonapps/ifscoatings\request_system\templatetags\template_filters.py", line 54, in filter_project
projects = safe_projects(project_list=_callable(MyDashboard(request=request)), request=request, filter_market=True)
File "D:/Pythonapps/ifscoatings\request_system\lib.py", line 73, in chemist_pending_work
projects = pending_or_onhold | pending_by_manager
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 234, in __or__
combined.query.combine(other.query, sql.OR)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 544, in combine
raise ValueError("When merging querysets using 'or', you "
ValueError: When merging querysets using 'or', you cannot have extra(select=...) on both sides.
[05/Dec/2013 17:37:05] "GET /dashboard/ HTTP/1.1" 500 655503
这是创建我不能拥有的错误的选择Exta。 这是已经看到的新错误。我只想合并两个查询集。
.....filter().extra(
select={"priority": "COALESCE(bm_rank, sales_rank, id)", "size": "order_size_price*order_size_weight"})
答案 0 :(得分:1)
仔细阅读,仔细阅读您发布的错误消息。 Django不支持使用额外选择合并QuerySets(两者都合并)。
我还没有看到你的其余代码,或者更多的代码让我能够了解你正在使用的上下文。
我建议您使用一些自定义生成器。然后再试一次。
您也可以尝试使用原始SQL。
答案 1 :(得分:0)
我想合并两个查询集并根据它们的优先级对它们进行排序。 所以这就是我想出来的。
result_list = sorted(chain(query_set_1, query_set_2),
key=lambda instance: instance.priority)
这里instance.priority
实际上是对记录进行排序。但结果实际上并不是我想要的查询集,但它对我有用。
我得到了List而不是Query set,但是我没有在组合和查询集之后执行的查询集操作。