#14196 closed New feature (fixed)
Objects that come from something_set, should have their parent object filled in
Reported by: | Mark Jones | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (11)
comment:1 by , 14 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 1.2 → SVN |
comment:2 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:3 by , 13 years ago
UI/UX: | unset |
---|
comment:5 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Assigning to myself for work.
comment:6 by , 10 years ago
I cannot reproduce this using the following test:
def test_prepopulated_parent_of_a_set(self): my_user = User.objects.create(username="MyUser") LogEntry.objects.create(user=my_user, action_flag=0) user_logs = my_user.logentry_set.all() first_log = user_logs[0] with self.assertNumQueries(0): log_user = first_log.user self.assertEquals(my_user.username, log_user.username)
This test passes, meaning no additional queries were executed when attempting to lookup the parent user. Maybe I'm missing something obvious?
comment:7 by , 10 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:8 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:10 by , 10 years ago
I'm betting over the course of the last 5 years this got fixed. Might be a good idea to add the test code to prevent regression though.
comment:11 by , 10 years ago
known_related_objects.tests.ExistingRelatedInstancesTests.test_foreign_key
tests this.
I couldn't find easily when it was introduced because the modeltests/regressiontests merge breaks git log.
Change UI/UX from NULL to False.