当我尝试在视图集中使用“Q”查询时,我面临以下错误。如果我在管理命令文件中使用它,它将工作没有任何问题。
我的观点。
@permission_classes((AllowAny,))
class ClipartViewSet(viewsets.GenericViewSet):
serializer_class = ClipartSerializer
queryset = Clipart.objects.filter(is_active=True).all()
def list(self, request, **kwargs):
# Some extra logic
# qs = Clipart.objects.filter(name="Apes") #This line will work without any issues
qs = Clipart.objects.filter(Q(name="Apes") | Q(name="Dog")) # This line will show error
print(qs)
return super(ClipartViewSet, self).list(self, request, **kwargs)
字符串
错误:
Internal Server Error: /api/s/configurator/cliparts
backend_1 | Traceback (most recent call last):
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
backend_1 | response = get_response(request)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
backend_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
backend_1 | return view_func(*args, **kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/rest_framework/viewsets.py", line 125, in view
backend_1 | return self.dispatch(request, *args, **kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 509, in dispatch
backend_1 | response = self.handle_exception(exc)
backend_1 | File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception
backend_1 | self.raise_uncaught_exception(exc)
backend_1 | File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
backend_1 | raise exc
backend_1 | File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 506, in dispatch
backend_1 | response = handler(request, *args, **kwargs)
backend_1 | File "/backend/mycomp/apps/ecommerce/configurator/views/design_views.py", line 109, in list
backend_1 | qs = Clipart.objects.filter(Q(name="Apes") | Q(name="Dog")) # This line will show error
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 85, in manager_method
backend_1 | return getattr(self.get_queryset(), name)(*args, **kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/safedelete/queryset.py", line 72, in filter
backend_1 | return super(SafeDeleteQueryset, queryset).filter(*args, **kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 941, in filter
backend_1 | return self._filter_or_exclude(False, args, kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 961, in _filter_or_exclude
backend_1 | clone._filter_or_exclude_inplace(negate, args, kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 968, in _filter_or_exclude_inplace
backend_1 | self._query.add_q(Q(*args, **kwargs))
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1391, in add_q
backend_1 | clause, _ = self._add_q(q_object, self.used_aliases)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1413, in _add_q
backend_1 | split_subq=split_subq, check_filterable=check_filterable,
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1281, in build_filter
backend_1 | arg, value = filter_expr
backend_1 | TypeError: cannot unpack non-iterable MatchAll object
型
1条答案
按热度按时间b4lqfgs41#
很可能你实现了错误的
Q
,你导入它:字符串
也就是说,你可以用**
__in
**lookup [Django-doc]来简化查询:型