models.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from __future__ import unicode_literals
  2. from django.contrib.auth.models import User
  3. from django.db import models
  4. from django.utils.encoding import python_2_unicode_compatible
  5. class MyFileField(models.FileField):
  6. pass
  7. @python_2_unicode_compatible
  8. class Member(models.Model):
  9. name = models.CharField(max_length=100)
  10. birthdate = models.DateTimeField(blank=True, null=True)
  11. gender = models.CharField(max_length=1, blank=True, choices=[('M', 'Male'), ('F', 'Female')])
  12. email = models.EmailField(blank=True)
  13. def __str__(self):
  14. return self.name
  15. @python_2_unicode_compatible
  16. class Band(models.Model):
  17. name = models.CharField(max_length=100)
  18. style = models.CharField(max_length=20)
  19. members = models.ManyToManyField(Member)
  20. def __str__(self):
  21. return self.name
  22. @python_2_unicode_compatible
  23. class Album(models.Model):
  24. band = models.ForeignKey(Band, models.CASCADE)
  25. name = models.CharField(max_length=100)
  26. cover_art = models.FileField(upload_to='albums')
  27. backside_art = MyFileField(upload_to='albums_back', null=True)
  28. def __str__(self):
  29. return self.name
  30. class HiddenInventoryManager(models.Manager):
  31. def get_queryset(self):
  32. return super(HiddenInventoryManager, self).get_queryset().filter(hidden=False)
  33. @python_2_unicode_compatible
  34. class Inventory(models.Model):
  35. barcode = models.PositiveIntegerField(unique=True)
  36. parent = models.ForeignKey('self', models.SET_NULL, to_field='barcode', blank=True, null=True)
  37. name = models.CharField(blank=False, max_length=20)
  38. hidden = models.BooleanField(default=False)
  39. # see #9258
  40. default_manager = models.Manager()
  41. objects = HiddenInventoryManager()
  42. def __str__(self):
  43. return self.name
  44. class Event(models.Model):
  45. main_band = models.ForeignKey(
  46. Band,
  47. models.CASCADE,
  48. limit_choices_to=models.Q(pk__gt=0),
  49. related_name='events_main_band_at',
  50. )
  51. supporting_bands = models.ManyToManyField(Band, blank=True, related_name='events_supporting_band_at')
  52. start_date = models.DateField(blank=True, null=True)
  53. start_time = models.TimeField(blank=True, null=True)
  54. description = models.TextField(blank=True)
  55. link = models.URLField(blank=True)
  56. min_age = models.IntegerField(blank=True, null=True)
  57. @python_2_unicode_compatible
  58. class Car(models.Model):
  59. owner = models.ForeignKey(User, models.CASCADE)
  60. make = models.CharField(max_length=30)
  61. model = models.CharField(max_length=30)
  62. def __str__(self):
  63. return "%s %s" % (self.make, self.model)
  64. class CarTire(models.Model):
  65. """
  66. A single car tire. This to test that a user can only select their own cars.
  67. """
  68. car = models.ForeignKey(Car, models.CASCADE)
  69. class Honeycomb(models.Model):
  70. location = models.CharField(max_length=20)
  71. class Bee(models.Model):
  72. """
  73. A model with a FK to a model that won't be registered with the admin
  74. (Honeycomb) so the corresponding raw ID widget won't have a magnifying
  75. glass link to select related honeycomb instances.
  76. """
  77. honeycomb = models.ForeignKey(Honeycomb, models.CASCADE)
  78. class Individual(models.Model):
  79. """
  80. A model with a FK to itself. It won't be registered with the admin, so the
  81. corresponding raw ID widget won't have a magnifying glass link to select
  82. related instances (rendering will be called programmatically in this case).
  83. """
  84. name = models.CharField(max_length=20)
  85. parent = models.ForeignKey('self', models.SET_NULL, null=True)
  86. soulmate = models.ForeignKey('self', models.CASCADE, null=True, related_name='soulmates')
  87. class Company(models.Model):
  88. name = models.CharField(max_length=20)
  89. class Advisor(models.Model):
  90. """
  91. A model with a m2m to a model that won't be registered with the admin
  92. (Company) so the corresponding raw ID widget won't have a magnifying
  93. glass link to select related company instances.
  94. """
  95. name = models.CharField(max_length=20)
  96. companies = models.ManyToManyField(Company)
  97. @python_2_unicode_compatible
  98. class Student(models.Model):
  99. name = models.CharField(max_length=255)
  100. def __str__(self):
  101. return self.name
  102. class Meta:
  103. ordering = ('name',)
  104. @python_2_unicode_compatible
  105. class School(models.Model):
  106. name = models.CharField(max_length=255)
  107. students = models.ManyToManyField(Student, related_name='current_schools')
  108. alumni = models.ManyToManyField(Student, related_name='previous_schools')
  109. def __str__(self):
  110. return self.name
  111. @python_2_unicode_compatible
  112. class Profile(models.Model):
  113. user = models.ForeignKey('auth.User', models.CASCADE, to_field='username')
  114. def __str__(self):
  115. return self.user.username