models.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from django.db import models
  2. from django.core.validators import MinValueValidator, MaxValueValidator
  3. import datetime
  4. class MyModel(models.Model):
  5. created_at = models.DateTimeField(auto_now_add=True)
  6. updated_at = models.DateTimeField(auto_now=True)
  7. class Meta:
  8. abstract = True
  9. class Country(MyModel):
  10. name = models.CharField("Название", max_length=200, unique=True)
  11. class Meta:
  12. ordering = ["name"]
  13. verbose_name = "Страна"
  14. verbose_name_plural = "Страны"
  15. def __str__(self):
  16. return self.name
  17. class Genre(MyModel):
  18. name = models.CharField("Название", max_length=200, unique=True)
  19. class Meta:
  20. ordering = ["name"]
  21. verbose_name = "Жанр"
  22. verbose_name_plural = "Жанры"
  23. def __str__(self):
  24. return self.name
  25. class Person(MyModel):
  26. name = models.CharField("Имя", max_length=400)
  27. origin_name = models.CharField("Имя в оригинале", max_length=400,
  28. blank=True, null=True)
  29. birthday = models.DateField("Дата рождения", blank=True, null=True,
  30. validators=[
  31. MaxValueValidator(
  32. limit_value=datetime.date.today)
  33. ])
  34. photo = models.ImageField(
  35. "Фото", upload_to='photos/', blank=True, null=True)
  36. kinopoisk_id = models.PositiveIntegerField(
  37. "Kinopoisk ID", blank=True, null=True)
  38. def age(self):
  39. if not self.birthday:
  40. return None
  41. today = datetime.date.today()
  42. return today.year - self.birthday.year \
  43. - ((today.month, today.day) < (self.birthday.month,
  44. self.birthday.day))
  45. class Meta:
  46. ordering = ["name"]
  47. verbose_name = "Персона"
  48. verbose_name_plural = "Персоны"
  49. def __str__(self):
  50. return self.name
  51. class Film(MyModel):
  52. name = models.CharField("Имя", max_length=1024)
  53. origin_name = models.CharField(
  54. "Название (в оригинале)", max_length=1024, blank=True, null=True)
  55. slogan = models.CharField("Девиз", max_length=2048, blank=True, null=True)
  56. country = models.ForeignKey(
  57. Country, on_delete=models.CASCADE, verbose_name="Страна")
  58. genres = models.ManyToManyField(Genre, verbose_name="Жанр")
  59. director = models.ForeignKey(
  60. Person, on_delete=models.CASCADE, verbose_name="Режиссер",
  61. related_name="directed_films")
  62. length = models.PositiveIntegerField(
  63. "Продолжительность", blank=True, null=True)
  64. year = models.PositiveIntegerField("Год выпуска", blank=True, null=True,
  65. validators=[MinValueValidator(
  66. limit_value=1885)])
  67. trailer_url = models.URLField("Трейлер", blank=True, null=True)
  68. cover = models.ImageField(
  69. "Постер", upload_to='covers/', blank=True, null=True)
  70. description = models.TextField("Описание", blank=True, null=True)
  71. people = models.ManyToManyField(Person, verbose_name="Актеры")
  72. kinopoisk_id = models.PositiveIntegerField(
  73. "Kinopoisk ID", blank=True, null=True)
  74. class Meta:
  75. ordering = ["name"]
  76. verbose_name = "Фильм"
  77. verbose_name_plural = "Фильмы"
  78. def __str__(self):
  79. return self.name