views:

61

answers:

1

Hi I'm looking to populate a list of members, based on where their club comes from.

This is my code:

 members = []
 if userprofile.countries.count() > 0:
     for c in userprofile.countries.all():
         clubs = Club.objects.filter(location__country = c)
         for club in clubs:
             members_list = Member.objects.get_members(club)
             for m in members_list:
                 members.append(m)

However, when evaluating for m in members_list: it throws an 'iteration over non-sequence'

I'm not entirely sure why? Can anyone give me any ideas?!

EDIT:

Solved using the following:

members = []
if userprofile.countries.count() > 0:
            members_list = member.objects.filter(memberstoentities__club__location__country__in = userprofile.countries.all())
            for m in members_list:
                members.append(m)
+2  A: 

Can't comment unless looking at Member model. But

  1. Can't we use .filter with back navigation, instead of get_members
  2. Do we need those many loops, and db access inside loop? ex:

clubs = Club.objects.filter(location_country_in = list_of_user_countries)

If your final list is list of members, you can do that as I mentioned above (at least in optimized way)

Narendra Kamma
Thanks! By compounding the queries and using your method, I've managed to solve it!
Once_functional