In [1]: from django.db import connection
In [2]: from testmodule.models import Parent, Child
In [3]: Child.objects.create(parent_int = 1, child_int = 2)
Out[3]: <Child: Child object>
In [4]: Child.objects.get(parent_int = 1, child_int = 2).parent_ptr
Out[4]: <Parent: Parent object>
In [5]: connection.queries
Out[5]:
[{'sql': u'INSERT INTO "testmodule_parent" ("parent_int") VALUES (1)',
'time': '0.001'},
{'sql': u'INSERT INTO "testmodule_child" ("parent_ptr_id", "child_int") VALUES (2, 2)',
'time': '0.000'},
{'sql': u'SELECT "testmodule_parent"."id", "testmodule_parent"."parent_int", "testmodule_child"."parent_ptr_id", "testmodule_child"."child_int" FROM "testmodule_child" INNER JOIN "testmodule_parent" ON ("testmodule_child"."parent_ptr_id" = "testmodule_parent"."id") WHERE ("testmodule_parent"."parent_int" = 1 AND "testmodule_child"."child_int" = 2 )',
'time': '0.000'},
{'sql': u'SELECT "testmodule_parent"."id", "testmodule_parent"."parent_int" FROM "testmodule_parent" WHERE "testmodule_parent"."id" = 2 ',
'time': '0.000'}]
In [6]: Child.objects.select_related('parent').get(parent_int = 1, child_int = 2).parent_ptr
Out[6]: <Parent: Parent object>
In [7]: connection.queries
Out[7]:
[{'sql': u'INSERT INTO "testmodule_parent" ("parent_int") VALUES (1)',
'time': '0.001'},
{'sql': u'INSERT INTO "testmodule_child" ("parent_ptr_id", "child_int") VALUES (2, 2)',
'time': '0.000'},
{'sql': u'SELECT "testmodule_parent"."id", "testmodule_parent"."parent_int", "testmodule_child"."parent_ptr_id", "testmodule_child"."child_int" FROM "testmodule_child" INNER JOIN "testmodule_parent" ON ("testmodule_child"."parent_ptr_id" = "testmodule_parent"."id") WHERE ("testmodule_parent"."parent_int" = 1 AND "testmodule_child"."child_int" = 2 )',
'time': '0.000'},
{'sql': u'SELECT "testmodule_parent"."id", "testmodule_parent"."parent_int" FROM "testmodule_parent" WHERE "testmodule_parent"."id" = 2 ',
'time': '0.000'},
{'sql': u'SELECT "testmodule_parent"."id", "testmodule_parent"."parent_int", "testmodule_child"."parent_ptr_id", "testmodule_child"."child_int" FROM "testmodule_child" INNER JOIN "testmodule_parent" ON ("testmodule_child"."parent_ptr_id" = "testmodule_parent"."id") WHERE ("testmodule_parent"."parent_int" = 1 AND "testmodule_child"."child_int" = 2 )',
'time': '0.000'},
{'sql': u'SELECT "testmodule_parent"."id", "testmodule_parent"."parent_int" FROM "testmodule_parent" WHERE "testmodule_parent"."id" = 2 ',
'time': '0.000'}]
Ok - thanks for the extra debug information. Looks like there is something going wrong here. As I indicated on #14371, the select_related() isn't necessary, but you've clearly shown that the parent object isn't being prepopulated as I said, either.
As a matter of procedure -- this is the sort of situation where it would have been ok to reopen the old ticket. It's the same problem, but by providing more debugging information you can demonstrate that the assertion I made was wrong. If that's the case, its perfectly acceptable to reopen an old ticket.