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