| 220 | |
| 221 | # model hierarchy, for ticket #8106 |
| 222 | class User(models.Model): |
| 223 | name = models.CharField(max_length=200) |
| 224 | |
| 225 | class Person(models.Model): |
| 226 | user = models.ForeignKey("User", unique=True) |
| 227 | |
| 228 | class Organizer(models.Model): |
| 229 | person = models.ForeignKey("Person") |
| 230 | |
| 231 | class Student(models.Model): |
| 232 | person = models.ForeignKey("Person") |
| 233 | |
| 234 | class Class(models.Model): |
| 235 | org = models.ForeignKey("Organizer") |
| 236 | |
| 237 | class Enrollment(models.Model): |
| 238 | std = models.ForeignKey("Student") |
| 239 | claz = models.ForeignKey("Class") |
| 240 | |
| 241 | |
| 242 | |
| 913 | Bug #8106 -- |
| 914 | >>> us = User(name="std") |
| 915 | >>> us.save() |
| 916 | >>> usp = Person(user=us) |
| 917 | >>> usp.save() |
| 918 | |
| 919 | >>> uo = User(name="org") |
| 920 | >>> uo.save() |
| 921 | >>> uop = Person(user=uo) |
| 922 | >>> uop.save() |
| 923 | |
| 924 | >>> s = Student(person = usp) |
| 925 | >>> s.save() |
| 926 | >>> o = Organizer(person = uop) |
| 927 | >>> o.save() |
| 928 | |
| 929 | >>> c = Class(org=o) |
| 930 | >>> c.save() |
| 931 | >>> e = Enrollment(std=s, claz=c) |
| 932 | >>> e.save() |
| 933 | |
| 934 | >>> e_related = Enrollment.objects.all().select_related()[0] |
| 935 | >>> print e_related.std.person.user.name |
| 936 | std |
| 937 | >>> print e_related.claz.org.person.user.name |
| 938 | org |
| 939 | |