1
from django.db import models
3
class Building(models.Model):
4
name = models.CharField(max_length=10)
7
return u"Building: %s" % self.name
9
class Device(models.Model):
10
building = models.ForeignKey('Building')
11
name = models.CharField(max_length=10)
13
def __unicode__(self):
14
return u"device '%s' in building %s" % (self.name, self.building)
16
class Port(models.Model):
17
device = models.ForeignKey('Device')
18
port_number = models.CharField(max_length=10)
20
def __unicode__(self):
21
return u"%s/%s" % (self.device.name, self.port_number)
23
class Connection(models.Model):
24
start = models.ForeignKey(Port, related_name='connection_start',
26
end = models.ForeignKey(Port, related_name='connection_end', unique=True)
28
def __unicode__(self):
29
return u"%s to %s" % (self.start, self.end)
31
# Another non-tree hierarchy that exercises code paths similar to the above
32
# example, but in a slightly different configuration.
33
class TUser(models.Model):
34
name = models.CharField(max_length=200)
36
class Person(models.Model):
37
user = models.ForeignKey(TUser, unique=True)
39
class Organizer(models.Model):
40
person = models.ForeignKey(Person)
42
class Student(models.Model):
43
person = models.ForeignKey(Person)
45
class Class(models.Model):
46
org = models.ForeignKey(Organizer)
48
class Enrollment(models.Model):
49
std = models.ForeignKey(Student)
50
cls = models.ForeignKey(Class)
52
# Models for testing bug #8036.
53
class Country(models.Model):
54
name = models.CharField(max_length=50)
56
class State(models.Model):
57
name = models.CharField(max_length=50)
58
country = models.ForeignKey(Country)
60
class ClientStatus(models.Model):
61
name = models.CharField(max_length=50)
63
class Client(models.Model):
64
name = models.CharField(max_length=50)
65
state = models.ForeignKey(State, null=True)
66
status = models.ForeignKey(ClientStatus)
68
# Some model inheritance exercises
69
class Parent(models.Model):
70
name = models.CharField(max_length=10)
72
def __unicode__(self):
76
value = models.IntegerField()
78
class Item(models.Model):
79
name = models.CharField(max_length=10)
80
child = models.ForeignKey(Child, null=True)
82
def __unicode__(self):
85
__test__ = {'API_TESTS': """
86
Regression test for bug #7110. When using select_related(), we must query the
87
Device and Building tables using two different aliases (each) in order to
88
differentiate the start and end Connection fields. The net result is that both
89
the "connections = ..." queries here should give the same results without
90
pulling in more than the absolute minimum number of tables (history has
91
shown that it's easy to make a mistake in the implementation and include some
92
unnecessary bonus joins).
94
>>> b=Building.objects.create(name='101')
95
>>> dev1=Device.objects.create(name="router", building=b)
96
>>> dev2=Device.objects.create(name="switch", building=b)
97
>>> dev3=Device.objects.create(name="server", building=b)
98
>>> port1=Port.objects.create(port_number='4',device=dev1)
99
>>> port2=Port.objects.create(port_number='7',device=dev2)
100
>>> port3=Port.objects.create(port_number='1',device=dev3)
101
>>> c1=Connection.objects.create(start=port1, end=port2)
102
>>> c2=Connection.objects.create(start=port2, end=port3)
104
>>> connections=Connection.objects.filter(start__device__building=b, end__device__building=b).order_by('id')
105
>>> [(c.id, unicode(c.start), unicode(c.end)) for c in connections]
106
[(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')]
108
>>> connections=Connection.objects.filter(start__device__building=b, end__device__building=b).select_related().order_by('id')
109
>>> [(c.id, unicode(c.start), unicode(c.end)) for c in connections]
110
[(1, u'router/4', u'switch/7'), (2, u'switch/7', u'server/1')]
112
# This final query should only join seven tables (port, device and building
113
# twice each, plus connection once).
114
>>> connections.query.count_active_tables()
117
Regression test for bug #8106. Same sort of problem as the previous test, but
118
this time there are more extra tables to pull in as part of the
119
select_related() and some of them could potentially clash (so need to be kept
122
>>> us = TUser.objects.create(name="std")
123
>>> usp = Person.objects.create(user=us)
124
>>> uo = TUser.objects.create(name="org")
125
>>> uop = Person.objects.create(user=uo)
126
>>> s = Student.objects.create(person = usp)
127
>>> o = Organizer.objects.create(person = uop)
128
>>> c = Class.objects.create(org=o)
129
>>> e = Enrollment.objects.create(std=s, cls=c)
131
>>> e_related = Enrollment.objects.all().select_related()[0]
132
>>> e_related.std.person.user.name
134
>>> e_related.cls.org.person.user.name
137
Regression test for bug #8036: the first related model in the tests below
138
("state") is empty and we try to select the more remotely related
139
state__country. The regression here was not skipping the empty column results
140
for country before getting status.
142
>>> australia = Country.objects.create(name='Australia')
143
>>> active = ClientStatus.objects.create(name='active')
144
>>> client = Client.objects.create(name='client', status=active)
147
<ClientStatus: ClientStatus object>
148
>>> Client.objects.select_related()[0].status
149
<ClientStatus: ClientStatus object>
150
>>> Client.objects.select_related('state')[0].status
151
<ClientStatus: ClientStatus object>
152
>>> Client.objects.select_related('state', 'status')[0].status
153
<ClientStatus: ClientStatus object>
154
>>> Client.objects.select_related('state__country')[0].status
155
<ClientStatus: ClientStatus object>
156
>>> Client.objects.select_related('state__country', 'status')[0].status
157
<ClientStatus: ClientStatus object>
158
>>> Client.objects.select_related('status')[0].status
159
<ClientStatus: ClientStatus object>
161
Exercising select_related() with multi-table model inheritance.
162
>>> c1 = Child.objects.create(name="child1", value=42)
163
>>> _ = Item.objects.create(name="item1", child=c1)
164
>>> _ = Item.objects.create(name="item2")
165
>>> Item.objects.select_related("child").order_by("name")
166
[<Item: item1>, <Item: item2>]