1 | #
|
---|
2 | # models.py
|
---|
3 | #
|
---|
4 |
|
---|
5 | from django.db import models
|
---|
6 |
|
---|
7 | class Vehicle(models.Model):
|
---|
8 | name = models.TextField()
|
---|
9 |
|
---|
10 | def __unicode__(self):
|
---|
11 | return u"A vehicle called %s" % self.name
|
---|
12 |
|
---|
13 | class Car(Vehicle):
|
---|
14 | wheels = models.PositiveIntegerField(default=4)
|
---|
15 |
|
---|
16 | def __unicode__(self):
|
---|
17 | return u"A car called %(name)s with %(wheels)s wheels" % \
|
---|
18 | {'name': self.name, 'wheels': self.wheels}
|
---|
19 |
|
---|
20 | class Boat(Vehicle):
|
---|
21 | has_sail = models.BooleanField(default=True)
|
---|
22 |
|
---|
23 | def __unicode__(self):
|
---|
24 | with_or_without = self.has_sail and "with" or "without"
|
---|
25 | return u"A boat called %(name)s %(with_or_without)s a sail" % \
|
---|
26 | {'name': self.name, 'with_or_without': with_or_without }
|
---|
27 |
|
---|
28 | class Amphibian(Car, Boat):
|
---|
29 | is_a_robot_in_disguise = models.BooleanField(default=False)
|
---|
30 |
|
---|
31 | def __unicode__(self):
|
---|
32 | return u"An ampibian which is 1) %(car)s and 2) %(boat)s" % \
|
---|
33 | {'car': Car.__unicode__(self),
|
---|
34 | 'boat': Boat.__unicode__(self)}
|
---|
35 |
|
---|
36 |
|
---|
37 | #
|
---|
38 | # The code to generate the shell results.
|
---|
39 | #
|
---|
40 |
|
---|
41 | from your.path.to.models import *
|
---|
42 |
|
---|
43 | Vehicle.objects.all().delete()
|
---|
44 | Boat(name="dingy", has_sail=False).save()
|
---|
45 | dingy_vehicle = Vehicle.objects.get(name="dingy")
|
---|
46 | dingy_vehicle.car
|
---|
47 | dingy_vehicle.boat
|
---|
48 | dingy_vehicle.car
|
---|
49 | dingy_vehicle.boat
|
---|
50 | Car(name="my rolls", wheels=4).save()
|
---|
51 | rolls_vehicle = Vehicle.objects.get(name="my rolls")
|
---|
52 | rolls_vehicle.car
|
---|
53 | rolls_vehicle.boat
|
---|
54 | rolls_vehicle.car
|
---|
55 | rolls_vehicle.boat
|
---|
56 | amphibian = Amphibian()
|
---|
57 | amphibian.name = "optimus prime"
|
---|
58 | amphibian.has_sail = True
|
---|
59 | amphibian.wheels = 23
|
---|
60 | amphibian.is_a_robot_in_disguise = True
|
---|
61 | amphibian.save()
|
---|
62 | robot_vehicle = Vehicle.objects.get(name="optimus prime")
|
---|
63 | robot_vehicle.car
|
---|
64 | robot_vehicle.boat
|
---|
65 | robot_vehicle.car
|
---|
66 | robot_vehicle.boat
|
---|
67 | robot_vehicle.car.amphibian
|
---|
68 | robot_vehicle.boat.amphibian
|
---|
69 |
|
---|
70 | #
|
---|
71 | # Shell results before the code was changed
|
---|
72 | #
|
---|
73 |
|
---|
74 | >>> Vehicle.objects.all().delete()
|
---|
75 | >>> Boat(name="dingy", has_sail=False).save()
|
---|
76 | >>> dingy_vehicle = Vehicle.objects.get(name="dingy")
|
---|
77 | >>> dingy_vehicle.car
|
---|
78 | Traceback (most recent call last):
|
---|
79 | ...
|
---|
80 | DoesNotExist: Car matching query does not exist.
|
---|
81 | >>> dingy_vehicle.boat
|
---|
82 | <Boat: A boat called dingy without a sail>
|
---|
83 | >>> dingy_vehicle.car # should get error
|
---|
84 | <Boat: A boat called dingy without a sail>
|
---|
85 | >>> dingy_vehicle.boat
|
---|
86 | <Boat: A boat called dingy without a sail>
|
---|
87 | >>> Car(name="my rolls", wheels=4).save()
|
---|
88 | >>> rolls_vehicle = Vehicle.objects.get(name="my rolls")
|
---|
89 | >>> rolls_vehicle.car
|
---|
90 | <Car: A car called my rolls with 4 wheels>
|
---|
91 | >>> rolls_vehicle.boat # should get error
|
---|
92 | <Car: A car called my rolls with 4 wheels>
|
---|
93 | >>> rolls_vehicle.car
|
---|
94 | <Car: A car called my rolls with 4 wheels>
|
---|
95 | >>> rolls_vehicle.boat # should get error
|
---|
96 | <Car: A car called my rolls with 4 wheels>
|
---|
97 | >>> amphibian = Amphibian()
|
---|
98 | >>> amphibian.name = "optimus prime"
|
---|
99 | >>> amphibian.has_sail = True
|
---|
100 | >>> amphibian.wheels = 23
|
---|
101 | >>> amphibian.is_a_robot_in_disguise = True
|
---|
102 | >>> amphibian.save()
|
---|
103 | >>> robot_vehicle = Vehicle.objects.get(name="optimus prime")
|
---|
104 | >>> robot_vehicle.car
|
---|
105 | <Car: A car called optimus prime with 23 wheels>
|
---|
106 | >>> robot_vehicle.boat # wrong object we want to see the boat part of optimus prime
|
---|
107 | <Car: A car called optimus prime with 23 wheels>
|
---|
108 | >>> robot_vehicle.car
|
---|
109 | <Car: A car called optimus prime with 23 wheels>
|
---|
110 | >>> robot_vehicle.boat # wrong object we want to see the boat part of optimus prime
|
---|
111 | <Car: A car called optimus prime with 23 wheels>
|
---|
112 | >>> robot_vehicle.car.amphibian
|
---|
113 | <Amphibian: An ampibian which is 1) A car called optimus prime with 23 wheels and 2) A boat called optimus prime with a sail>
|
---|
114 | >>> robot_vehicle.boat.amphibian
|
---|
115 | <Amphibian: An ampibian which is 1) A car called optimus prime with 23 wheels and 2) A boat called optimus prime with a sail>
|
---|
116 |
|
---|
117 | #
|
---|
118 | # Shell results after the code was changed
|
---|
119 | #
|
---|
120 |
|
---|
121 | >>> Vehicle.objects.all().delete()
|
---|
122 | >>> Boat(name="dingy", has_sail=False).save()
|
---|
123 | >>> dingy_vehicle = Vehicle.objects.get(name="dingy")
|
---|
124 | >>> dingy_vehicle.car # the dingy is not a car
|
---|
125 | Traceback (most recent call last):
|
---|
126 | ...
|
---|
127 | DoesNotExist: Car matching query does not exist.
|
---|
128 | >>> dingy_vehicle.boat
|
---|
129 | <Boat: A boat called dingy without a sail>
|
---|
130 | >>> dingy_vehicle.car # the dingy is still not a car
|
---|
131 | Traceback (most recent call last):
|
---|
132 | ...
|
---|
133 | DoesNotExist: Car matching query does not exist.
|
---|
134 | >>> dingy_vehicle.boat
|
---|
135 | <Boat: A boat called dingy without a sail>
|
---|
136 | >>> Car(name="my rolls", wheels=4).save()
|
---|
137 | >>> rolls_vehicle = Vehicle.objects.get(name="my rolls")
|
---|
138 | >>> rolls_vehicle.car
|
---|
139 | <Car: A car called my rolls with 4 wheels>
|
---|
140 | >>> rolls_vehicle.boat
|
---|
141 | Traceback (most recent call last):
|
---|
142 | ...
|
---|
143 | DoesNotExist: Boat matching query does not exist.
|
---|
144 | >>> rolls_vehicle.car
|
---|
145 | <Car: A car called my rolls with 4 wheels>
|
---|
146 | >>> rolls_vehicle.boat
|
---|
147 | Traceback (most recent call last):
|
---|
148 | ...
|
---|
149 | DoesNotExist: Boat matching query does not exist.
|
---|
150 | >>> amphibian = Amphibian()
|
---|
151 | >>> amphibian.name = "optimus prime"
|
---|
152 | >>> amphibian.has_sail = True
|
---|
153 | >>> amphibian.wheels = 23
|
---|
154 | >>> amphibian.is_a_robot_in_disguise = True
|
---|
155 | >>> amphibian.save()
|
---|
156 | >>> robot_vehicle = Vehicle.objects.get(name="optimus prime")
|
---|
157 | >>> robot_vehicle.car
|
---|
158 | <Car: A car called optimus prime with 23 wheels>
|
---|
159 | >>> robot_vehicle.boat
|
---|
160 | <Boat: A boat called optimus prime with a sail>
|
---|
161 | >>> robot_vehicle.car
|
---|
162 | <Car: A car called optimus prime with 23 wheels>
|
---|
163 | >>> robot_vehicle.boat
|
---|
164 | <Boat: A boat called optimus prime with a sail>
|
---|
165 | >>> robot_vehicle.car.amphibian
|
---|
166 | <Amphibian: An ampibian which is 1) A car called optimus prime with 23 wheels and 2) A boat called optimus prime with a sail>
|
---|
167 | >>> robot_vehicle.boat.amphibian
|
---|
168 | <Amphibian: An ampibian which is 1) A car called optimus prime with 23 wheels and 2) A boat called optimus prime with a sail>
|
---|