QCombination对象不可迭代

2024-10-16 20:53:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在执行查询时遇到问题,它会给我错误。但是如果从django控制台运行,我会正确返回结果

在python shell中(python管理.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>]

在视图.py在

^{pr2}$

错误:“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

Tags: djangoinpydbmodelslibpackagessite
2条回答

既然没人接电话,我就试试看。确保导入正确的User。在Django documentation中,它是这样导入的:

from django.contrib.auth.models import User

我们可以看到您在shell中使用了from api.models import User,但是我们没有看到您如何在视图中导入User。我知道它可以在shell中使用“bad”导入,但是仍然要确保在这两种情况下都使用正确的导入并告诉我们结果。如果还是不行,我不知道,对不起!在

就像@Nil说的,但是检查一下qimport视图.py. 它可能不是django.db.models.Q!在

相关问题 更多 >