models.py 4.0 KB

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