Skip to main content
 首页 » 编程设计

python之“经理”对象不是可迭代的错误

2025年05月04日77yxwkf

我正在尝试做一个简单的查询,但我得到了 'Manager' object is not iterable error.

我的代码(相关部分):

def sort(request): 
    sort_type = request.GET.get('srt', '') 
    q = Question.objects 
    if sort_type == 'views': 
        q.order_by('-views') 
    q.all() 
    return render(request, 'questions/index.html',{ 
        'questions': q 
    }) 

我做错了什么?

请您参考如下方法:

你的代码应该是

def sort(request): 
    sort_type = request.GET.get('srt', '') 
    q = Question.objects.all() 
    if sort_type == 'views': 
        q = q.order_by('-views') 
    return render(request, 'questions/index.html',{ 
        'questions': q 
    }) 

您需要将 q.order_by('-views') 分配给 q 以便更新您的查询集。