views:

26

answers:

1

I'm trying to populate a list based on partiular members, but it doesn't seem to be going to plan.

user = request.user
    members = [] 
    userprofile = user.get_profile()
    if user.is_superuser or user.is_staff:
        if userprofile.countries.count() == 0:
            members = Member.objects.all()
        elif userprofile.countries.count() >0:
            for c in userprofile.countries.all():
                m1 = Member.objects.filter(location__country = c)
                members.append(m1)
        else:
            pass

    self.fields['members'].choices = [(member.id, member.display_name()) for member in members]

Here, we see self.fields (it's a multi-select box) has member.id. I've tried both this and member.pk, but it doesn't seem to be working => Django informs me member has no attributes called id or pk

If a user is a superuser and has a countries count of 0, then It works fine; So i know its to do with the append function underneath the queryset call.

Could anyone offer any hints as to why id/pk is unavailable/lost after adding the results to a list? In addition, does anyone know of a workaround?

+1  A: 

I think your problem is that Member.objects.filter(location__country = c) is going to return a QuerySet object... which is essentially a hook to execute a query (lazily) once it is evaluated. if you change that to list(Member.objects.filter(location__country = c)) it will be evaluated immediately and give you back a list of Member model instances instead of a QuerySet object.

Matthew J Morrison