我在執行查詢時遇到問題,它給了我錯誤。但是,如果從控制檯Django的運行,我回到正確'QCombination'對象不可迭代
結果在Python Shell(蟒蛇manage.py殼)
>>> from api.models import User
>>> from django.db.models import Q
>>> User.objects.filter(Q(username__contains='lucas'))
[<User: @lucas>, <User: @lucasfuentes>, <User: @lucas_gasolero>]
在views.py
def search(req, keyword):
users = User.objects.filter(
Q(username__contains='lucas') | Q(name__contains='lucas')
)
[..]
ERROR: 'QCombination'對象是不可迭代的
Traceback:
File "[..]/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "[..]/api/views.py" in search
84. Q(username__contains='lucas') | Q(name__contains='lucas')
File "[..]/lib/python2.7/site-packages/django/db/models/manager.py" in filter
155. return self.get_query_set().filter(*args, **kwargs)
File "[..]lib/python2.7/site-packages/django/db/models/query.py" in filter
669. return self._filter_or_exclude(False, *args, **kwargs)
File "[..]lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
687. clone.query.add_q(Q(*args, **kwargs))
File "[..]lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1271. can_reuse=used_aliases, force_having=force_having)
File "[..]lib/python2.7/site-packages/django/db/models/sql/query.py" in add_filter
1066. arg, value = filter_expr
Exception Type: TypeError at /v1/search/as/
Exception Value: 'QCombination' object is not iterable
在shell中它不是同一個查詢。你有沒有在shell中嘗試過Q(username__contains ='lucas')| Q(name__contains = '盧卡斯')'? – warvariuc
是一樣的。在views.py'Q(username__contains ='lucas') - 錯誤'Q'對象不可迭代。在shell中'Q(username__contains ='lucas')| Q(name__contains ='lucas')'沒有錯誤! :( – lucasmg
@lucasmg,你是否仍然有同樣的問題? – Nil