Skip to content

Instantly share code, notes, and snippets.

@martinsam
Created October 28, 2010 12:04
Show Gist options
  • Save martinsam/651211 to your computer and use it in GitHub Desktop.
Save martinsam/651211 to your computer and use it in GitHub Desktop.
User Perf
#don't multiply queryset for user.get_profile...
#coworkers is a queryset of User
coworkers_id = set([c.id for c in coworkers])
from users_profiles.models import UserProfile
profiles = UserProfile.objects.filter(user__in=coworkers_id)
co_profiles = dict((u.id, u) for u in profiles)
for co in coworkers:
co.get_profile = co_profiles.get(co.id)
return coworkers
@martinsam
Copy link
Author

    coworkers_id = set([c.id for c in coworkers])
    from users_profiles.models import UserProfile
    co_profiles = UserProfile.objects.in_bulk(coworkers_id)

    for co in coworkers:
       co.get_profile = co_profiles.get(co.id)
    return coworkers

@martinsam
Copy link
Author

Via 'RawQuerySet', however, impossible to chain with "queryset" methods

return coworkers.objects.raw('SELECT * FROM auth_user INNER JOIN users_profiles_userprofile ON (users_profiles_userprofile.user_id = auth_user.id)')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment