caldf
caldf

Reputation: 71

Filtering a queryset issue in view django

I apologise in advance as im still new to django and I've inherited this projects.

i currently have this 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)

I get a list of users from matching app in "matches = " like this from the print (matches) [[<User: lucy>, '79.00%'], [<User: newuser>, '70.71%'],......

and i get a list of users from the "users =" like this from print (users) [<User: jonsnow>, <User: newuser>]......

how do i filter matches down to the users that come through, i tried adding a .filter(User=users) on the end of "matches =" however i got this error "'list' object has no attribute 'filter'".

I really hope this makes some sense i do apologise if I've explained my self very badly here, and thank you in advance for any help.

Upvotes: 0

Views: 54

Answers (2)

panatale1
panatale1

Reputation: 373

You could use a list comprehension. They're fairly quick, execution time-wise. The following should do the trick:

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

OR

user_ids = users.values_list('id', flat=True)
matches = [match for match in matches if match[0].id in user_ids]

Upvotes: 1

olofom
olofom

Reputation: 6471

get_matches_with_percent is not a django method, but a custom method in your models manager. When searching the internet I assume that this is your code, and specifically your method. It does not return a QuerySet but a regular list. You will have to update that method if you want to be able to filter the QuerySet instead.

Upvotes: 1

Related Questions