6
from django.db import models
8
class Place(models.Model):
9
name = models.CharField(maxlength=50)
10
address = models.CharField(maxlength=80)
13
return "%s the place" % self.name
15
class Restaurant(Place):
16
serves_hot_dogs = models.BooleanField()
17
serves_pizza = models.BooleanField()
20
return "%s the restaurant" % self.name
22
class ItalianRestaurant(Restaurant):
23
serves_gnocchi = models.BooleanField()
26
return "%s the italian restaurant" % self.name
29
# Make sure Restaurant has the right fields in the right order.
30
>>> [f.name for f in Restaurant._meta.fields]
31
['id', 'name', 'address', 'serves_hot_dogs', 'serves_pizza']
33
# Make sure ItalianRestaurant has the right fields in the right order.
34
>>> [f.name for f in ItalianRestaurant._meta.fields]
35
['id', 'name', 'address', 'serves_hot_dogs', 'serves_pizza', 'serves_gnocchi']
37
# Create a couple of Places.
38
>>> p1 = Place(name='Master Shakes', address='666 W. Jersey')
40
>>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
43
# Test constructor for Restaurant.
44
>>> r = Restaurant(name='Demon Dogs', address='944 W. Fullerton', serves_hot_dogs=True, serves_pizza=False)
47
# Test the constructor for ItalianRestaurant.
48
>>> ir = ItalianRestaurant(name='Ristorante Miron', address='1234 W. Elm', serves_hot_dogs=False, serves_pizza=False, serves_gnocchi=True)