我有一个视图,它根据传入的技能从数据库中提取多个用户。它几乎按照需要工作,除非它返回多个用户,它只传回最近获取的用户。如何聚合提取的用户以传递回模板。我已经尝试将它们作为列表传回,但它们没有出现。
这是我的代码:
form = FilterFreelancerForm(request.POST)
filtered_skills = set((request.POST.getlist('skills_select')))
match_fl = Freelancer.object.annotate(c=Count('skills')).filter(c=len(filtered_skills))
candidate_freelancers = None
for skill in filtered_skills:
candidate_freelancers = match_fl.filter(skills=skill)
freelancers = None
for freelancer in candidate_freelancers:
freelancers = User.objects.filter(freelancer=freelancer.id)
return render(request, 'freelancestudent/browsefreelancers.html', {'freelancers': freelancers,
'filter_form': form})
我之前有这个:
freelancers = []
for freelancer in candidate_freelancers:
freelancers.append(User.objects.filter(freelancer=freelancer.id))
不向模板返回任何内容。
答案 0 :(得分:1)
而不是:
for freelancer in candidate_freelancers:
freelancers = User.objects.filter(freelancer=freelancer.id)
尝试:
freelancers = User.objects.filter(freelancer__in=[freelancer.id for freelancer in candidate_freelancers])
出:
[<User: user1>, <User: user2>]