PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 51ms app.codeStats 0ms

/tests/modeltests/model_inheritance/models.py

https://code.google.com/p/mango-py/
Python | 153 lines | 133 code | 1 blank | 19 comment | 2 complexity | 43889872e944b35b24d4dd857756704c MD5 | raw file
  1"""
  2XX. Model inheritance
  3
  4Model inheritance exists in two varieties:
  5    - abstract base classes which are a way of specifying common
  6      information inherited by the subclasses. They don't exist as a separate
  7      model.
  8    - non-abstract base classes (the default), which are models in their own
  9      right with their own database tables and everything. Their subclasses
 10      have references back to them, created automatically.
 11
 12Both styles are demonstrated here.
 13"""
 14
 15from django.db import models
 16
 17#
 18# Abstract base classes
 19#
 20
 21class CommonInfo(models.Model):
 22    name = models.CharField(max_length=50)
 23    age = models.PositiveIntegerField()
 24
 25    class Meta:
 26        abstract = True
 27        ordering = ['name']
 28
 29    def __unicode__(self):
 30        return u'%s %s' % (self.__class__.__name__, self.name)
 31
 32class Worker(CommonInfo):
 33    job = models.CharField(max_length=50)
 34
 35class Student(CommonInfo):
 36    school_class = models.CharField(max_length=10)
 37
 38    class Meta:
 39        pass
 40
 41class StudentWorker(Student, Worker):
 42    pass
 43
 44#
 45# Abstract base classes with related models
 46#
 47
 48class Post(models.Model):
 49    title = models.CharField(max_length=50)
 50
 51class Attachment(models.Model):
 52    post = models.ForeignKey(Post, related_name='attached_%(class)s_set')
 53    content = models.TextField()
 54
 55    class Meta:
 56        abstract = True
 57
 58    def __unicode__(self):
 59        return self.content
 60
 61class Comment(Attachment):
 62    is_spam = models.BooleanField()
 63
 64class Link(Attachment):
 65    url = models.URLField()
 66
 67#
 68# Multi-table inheritance
 69#
 70
 71class Chef(models.Model):
 72    name = models.CharField(max_length=50)
 73
 74    def __unicode__(self):
 75        return u"%s the chef" % self.name
 76
 77class Place(models.Model):
 78    name = models.CharField(max_length=50)
 79    address = models.CharField(max_length=80)
 80
 81    def __unicode__(self):
 82        return u"%s the place" % self.name
 83
 84class Rating(models.Model):
 85    rating = models.IntegerField(null=True, blank=True)
 86
 87    class Meta:
 88        abstract = True
 89        ordering = ['-rating']
 90
 91class Restaurant(Place, Rating):
 92    serves_hot_dogs = models.BooleanField()
 93    serves_pizza = models.BooleanField()
 94    chef = models.ForeignKey(Chef, null=True, blank=True)
 95
 96    class Meta(Rating.Meta):
 97        db_table = 'my_restaurant'
 98
 99    def __unicode__(self):
100        return u"%s the restaurant" % self.name
101
102class ItalianRestaurant(Restaurant):
103    serves_gnocchi = models.BooleanField()
104
105    def __unicode__(self):
106        return u"%s the italian restaurant" % self.name
107
108class Supplier(Place):
109    customers = models.ManyToManyField(Restaurant, related_name='provider')
110
111    def __unicode__(self):
112        return u"%s the supplier" % self.name
113
114class ParkingLot(Place):
115    # An explicit link to the parent (we can control the attribute name).
116    parent = models.OneToOneField(Place, primary_key=True, parent_link=True)
117    main_site = models.ForeignKey(Place, related_name='lot')
118
119    def __unicode__(self):
120        return u"%s the parking lot" % self.name
121
122#
123# Abstract base classes with related models where the sub-class has the
124# same name in a different app and inherits from the same abstract base
125# class.
126# NOTE: The actual API tests for the following classes are in
127#       model_inheritance_same_model_name/models.py - They are defined
128#       here in order to have the name conflict between apps
129#
130
131class Title(models.Model):
132    title = models.CharField(max_length=50)
133
134class NamedURL(models.Model):
135    title = models.ForeignKey(Title, related_name='attached_%(app_label)s_%(class)s_set')
136    url = models.URLField()
137
138    class Meta:
139        abstract = True
140
141class Copy(NamedURL):
142    content = models.TextField()
143
144    def __unicode__(self):
145        return self.content
146
147class Mixin(object):
148    def __init__(self):
149        self.other_attr = 1
150        super(Mixin, self).__init__()
151
152class MixinModel(models.Model, Mixin):
153    pass