英文:
Python Django: TypeError: cannot unpack non-iterable MatchAll object
问题
我在尝试在视图集中使用 'Q' 查询时遇到以下错误。如果我在管理命令文件中使用它,就不会出现任何问题。
我的视图。
@permission_classes((AllowAny,))
class ClipartViewSet(viewsets.GenericViewSet):
serializer_class = ClipartSerializer
queryset = Clipart.objects.filter(is_active=True).all()
def list(self, request, **kwargs):
# 一些额外的逻辑
# qs = Clipart.objects.filter(name="Apes") #这一行将正常工作,没有问题
qs = Clipart.objects.filter(Q(name="Apes") | Q(name="Dog")) # 这一行将显示错误
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 this.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 = this.handle_exception(exc)
backend_1 | File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception
backend_1 | this.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")) # 这一行将显示错误
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 85, in manager_method
backend_1 | return getattr(this.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 this._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 | this._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, _ = this._add_q(q_object, this.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
英文:
I am facing below error when try to query using 'Q' in a viewset. It will work without any issues if I use this in a management command file.
My view.
@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)
Error:
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
得分: 0
你可能实现了错误的 Q
,你应该这样导入它:
from django.db.models import Q
@permission_classes((AllowAny,))
class ClipartViewSet(viewsets.GenericViewSet):
serializer_class = ClipartSerializer
queryset = Clipart.objects.filter(is_active=True).all()
def list(self, request, **kwargs):
qs = Clipart.objects.filter(Q(name='Apes') | Q(name='Dog'))
return super(ClipartViewSet, self).list(self, request, **kwargs)
话虽如此,你可以使用 __in
查找 <sup>[Django-doc]</sup> 来简化查询:
@permission_classes((AllowAny,))
class ClipartViewSet(viewsets.GenericViewSet):
serializer_class = ClipartSerializer
queryset = Clipart.objects.filter(is_active=True).all()
def list(self, request, **kwargs):
# 一些额外的逻辑
qs = Clipart.objects.filter(name__in=('Apes', 'Dog'))
print(qs)
return super(ClipartViewSet, self).list(self, request, **kwargs)
英文:
Likely you implement the wrong Q
, you import this with:
<pre><code>from <b>django.db.models</b> import Q
@permission_classes((AllowAny,))
class ClipartViewSet(viewsets.GenericViewSet):
serializer_class = ClipartSerializer
queryset = Clipart.objects.filter(is_active=True).all()
def list(self, request, **kwargs):
qs = Clipart.objects.filter(Q(name='Apes') | Q(name='Dog'))
return super(ClipartViewSet, self).list(self, request, **kwargs)</code></pre>
that being said, you can simplify the query with an __in
lookup <sup>[Django-doc]</sup>:
<pre><code>@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(<b>name__in=('Apes', 'Dog')</b>)
print(qs)
return super(ClipartViewSet, self).list(self, request, **kwargs)</code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论