在视图django中过滤查询集问题

时间:2017-05-16 21:03:21

标签: python django

我提前道歉,因为我仍然是django的新手,我继承了这个项目。

我目前有这个views.py

def home(request):
    if request.user.is_authenticated():
        location = request.GET.get('location', request.user.profile.location)
        users = User.objects.filter(profile__location=location) 
        print (users)
        matches = Match.objects.get_matches_with_percent(request.user)
        print (matches)

我从“匹配”中的匹配应用中获得了一个用户列表(匹配)[[<User: lucy>, '79.00%'], [<User: newuser>, '70.71%'],......

我从print(users)[<User: jonsnow>, <User: newuser>]......

获取“users =”中的用户列表

如何将匹配过滤到通过的用户,我尝试在“matches =”的末尾添加.filter(User = users)但是我收到此错误“'list'对象没有属性'过滤器“”。

我真的希望如果我在这里解释得非常糟糕,我会道歉,并提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

get_matches_with_percent不是django方法,而是模型管理器中的自定义方法。在互联网上搜索时,我假设this is your code,特别是你的方法。它不返回QuerySet而是返回常规列表。如果您希望能够过滤QuerySet,则必须更新该方法。

答案 1 :(得分:1)

您可以使用列表理解。它们相当快,执行时间紧迫。以下应该可以解决问题:

T[]

matches = [match for match in matches if match[0] in users]