﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14196	Objects that come from something_set, should have their parent object filled in	Mark Jones		"{{{
#!python
def avatar_url(user, size=80):
    if not isinstance(user, User):
        try:
            user = User.objects.get(username=user)
        except User.DoesNotExist:
            return AVATAR_DEFAULT_URL
    avatars = user.avatar_set.order_by('-date_uploaded')
    primary = avatars.filter(primary=True)
    if primary.count() > 0:
        avatar = primary[0]
    elif avatars.count() > 0:
        avatar = avatars[0]
    else:
        avatar = None
    if avatar is not None:
        avatar.user = user # prevent an extra lookup because the avatar doesn't know he came from user.avatar_set
}}}

If I don't put in the last line in that sample, accessing avatar.user will again query the database to get the ""parent"" object.  It seems to me that when you do user.avatar_set.all(), the user in the avatar object would get set before the object is returned, but that isn't the case, instead it loads another user object from the db and stuffs it into the avatar object.

"	New feature	closed	Database layer (models, ORM)	dev	Normal	fixed			Accepted	0	0	0	0	0	0
