Hi, I have the following code:
results = list(results.filter(**location_distance_kwargs))
for trip in possible_trips:
try:
trip = Trip.objects.get(id=trip.id, **trip_kwargs)
profile = Profile.objects.get(user=trip.user)
if profile not in results:
results.append(profile)
except Trip.DoesNotExist:
pass
In the above, "results" is a GeoQuerySet of Profile objects, converted
to a list so that i can append more profiles in the code below... I
then check a totally separate array (of Trip objects) against their
own kwargs, then for each match I pull out the Profile object
associated with the User of each Trip, and append it to the "results"
list, if it's not already there (to avoid duplication).
As an aside: I first tried appending profiles without having converted
"results" to a list, but append() couldnt be used on the "results"
querySet. so i forced "results" to be a list using the list()
function, which made it possible to append the second profiles onto
the end of results.
All seemed ok so far. However, i finally wanted to order the profiles
in results by "user__last_login" (profiles have a ForeignKey of user,
and user has a property "last login". When i tried doing this using
python's sorted() function it didnt work as it seemed unable to delve
beyond the user foreign key and into the last_login attribute. this is
what failed:
results = sorted(results, key=attrgetter('user__last_login'),
reverse=True)
as obviously "user__last_login" doesnt exist at this stage as an
attribute. i tried "user.last_login" and varous combinations, but
nothing worked. at the moment the code just reads like this, which
works:
results = sorted(results, key=attrgetter('user'), reverse=True)
any idea how, after merging two query sets by turning them into lists,
i can sort them by an attribute of a foreign key for the objects in
the list?
Thanks in advance to anybody who can help!
Kyle
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.