使用排除时用户对象不可迭代

时间:2018-10-31 07:17:26

标签: python django

我的功能:

@login_required(login_url='/account/login/')
def SearchView(request):
    query = request.GET.get("search")
    # if query:
    profile = User.objects.filter(
    Q(is_client=True,first_name__icontains=query) |
    Q(is_client=True,last_name__icontains=query)

    ).exclude(request.user).distinct()
    hub = Hub.objects.filter(
        Q(name__icontains=query)
    )
    task = Task.objects.filter(
        Q(title__icontains=query),
    )
    results = list(
            chain(profile, hub, task))
            # key=lambda objects: objects.created_at
    paginator = Paginator(results, 5)  # Show 5 contacts per page
    page_request_var = "page"
    page = request.GET.get(page_request_var)
    contacts = paginator.get_page(page)
    notifications = Notification.objects.filter(receiver=request.user)

    context = {
        'profile':profile,
        'page_request_var':page_request_var,
        'contacts': contacts,

    }


    return render(request,'search.html',context)

我希望搜索结果排除当前用户。用户是作为个人资料发送的。问题是,当我使用排除时,其显示错误“用户对象不可迭代”。

1 个答案:

答案 0 :(得分:2)

<Router/>exclude一样,采用过滤器表达式。您不能只是传递一个对象;它需要一个查询和一个值。因此,您可以这样做:

filter