models.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. from django.contrib.contenttypes import generic
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.db import models
  4. from django.utils.encoding import python_2_unicode_compatible
  5. ## Basic tests
  6. @python_2_unicode_compatible
  7. class Author(models.Model):
  8. name = models.CharField(max_length=50, unique=True)
  9. first_book = models.ForeignKey('Book', related_name='first_time_authors')
  10. favorite_authors = models.ManyToManyField(
  11. 'self', through='FavoriteAuthors', symmetrical=False, related_name='favors_me')
  12. def __str__(self):
  13. return self.name
  14. class Meta:
  15. ordering = ['id']
  16. class AuthorWithAge(Author):
  17. author = models.OneToOneField(Author, parent_link=True)
  18. age = models.IntegerField()
  19. class FavoriteAuthors(models.Model):
  20. author = models.ForeignKey(Author, to_field='name', related_name='i_like')
  21. likes_author = models.ForeignKey(Author, to_field='name', related_name='likes_me')
  22. class Meta:
  23. ordering = ['id']
  24. @python_2_unicode_compatible
  25. class AuthorAddress(models.Model):
  26. author = models.ForeignKey(Author, to_field='name', related_name='addresses')
  27. address = models.TextField()
  28. class Meta:
  29. ordering = ['id']
  30. def __str__(self):
  31. return self.address
  32. @python_2_unicode_compatible
  33. class Book(models.Model):
  34. title = models.CharField(max_length=255)
  35. authors = models.ManyToManyField(Author, related_name='books')
  36. def __str__(self):
  37. return self.title
  38. class Meta:
  39. ordering = ['id']
  40. class BookWithYear(Book):
  41. book = models.OneToOneField(Book, parent_link=True)
  42. published_year = models.IntegerField()
  43. aged_authors = models.ManyToManyField(
  44. AuthorWithAge, related_name='books_with_year')
  45. @python_2_unicode_compatible
  46. class Reader(models.Model):
  47. name = models.CharField(max_length=50)
  48. books_read = models.ManyToManyField(Book, related_name='read_by')
  49. def __str__(self):
  50. return self.name
  51. class Meta:
  52. ordering = ['id']
  53. class BookReview(models.Model):
  54. book = models.ForeignKey(BookWithYear)
  55. notes = models.TextField(null=True, blank=True)
  56. ## Models for default manager tests
  57. class Qualification(models.Model):
  58. name = models.CharField(max_length=10)
  59. class Meta:
  60. ordering = ['id']
  61. class TeacherManager(models.Manager):
  62. def get_queryset(self):
  63. return super(TeacherManager, self).get_queryset().prefetch_related('qualifications')
  64. @python_2_unicode_compatible
  65. class Teacher(models.Model):
  66. name = models.CharField(max_length=50)
  67. qualifications = models.ManyToManyField(Qualification)
  68. objects = TeacherManager()
  69. def __str__(self):
  70. return "%s (%s)" % (self.name, ", ".join(q.name for q in self.qualifications.all()))
  71. class Meta:
  72. ordering = ['id']
  73. class Department(models.Model):
  74. name = models.CharField(max_length=50)
  75. teachers = models.ManyToManyField(Teacher)
  76. class Meta:
  77. ordering = ['id']
  78. ## GenericRelation/GenericForeignKey tests
  79. @python_2_unicode_compatible
  80. class TaggedItem(models.Model):
  81. tag = models.SlugField()
  82. content_type = models.ForeignKey(ContentType, related_name="taggeditem_set2")
  83. object_id = models.PositiveIntegerField()
  84. content_object = generic.GenericForeignKey('content_type', 'object_id')
  85. created_by_ct = models.ForeignKey(ContentType, null=True,
  86. related_name='taggeditem_set3')
  87. created_by_fkey = models.PositiveIntegerField(null=True)
  88. created_by = generic.GenericForeignKey('created_by_ct', 'created_by_fkey',)
  89. favorite_ct = models.ForeignKey(ContentType, null=True,
  90. related_name='taggeditem_set4')
  91. favorite_fkey = models.CharField(max_length=64, null=True)
  92. favorite = generic.GenericForeignKey('favorite_ct', 'favorite_fkey')
  93. def __str__(self):
  94. return self.tag
  95. class Bookmark(models.Model):
  96. url = models.URLField()
  97. tags = generic.GenericRelation(TaggedItem, related_name='bookmarks')
  98. favorite_tags = generic.GenericRelation(TaggedItem,
  99. content_type_field='favorite_ct',
  100. object_id_field='favorite_fkey',
  101. related_name='favorite_bookmarks')
  102. class Comment(models.Model):
  103. comment = models.TextField()
  104. # Content-object field
  105. content_type = models.ForeignKey(ContentType)
  106. object_pk = models.TextField()
  107. content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
  108. ## Models for lookup ordering tests
  109. class House(models.Model):
  110. address = models.CharField(max_length=255)
  111. class Meta:
  112. ordering = ['id']
  113. class Room(models.Model):
  114. name = models.CharField(max_length=50)
  115. house = models.ForeignKey(House, related_name='rooms')
  116. class Meta:
  117. ordering = ['id']
  118. class Person(models.Model):
  119. name = models.CharField(max_length=50)
  120. houses = models.ManyToManyField(House, related_name='occupants')
  121. @property
  122. def primary_house(self):
  123. # Assume business logic forces every person to have at least one house.
  124. return sorted(self.houses.all(), key=lambda house: -house.rooms.count())[0]
  125. class Meta:
  126. ordering = ['id']
  127. ## Models for nullable FK tests
  128. @python_2_unicode_compatible
  129. class Employee(models.Model):
  130. name = models.CharField(max_length=50)
  131. boss = models.ForeignKey('self', null=True,
  132. related_name='serfs')
  133. def __str__(self):
  134. return self.name
  135. class Meta:
  136. ordering = ['id']