from django.db import models from django.core.validators import MinValueValidator, MaxValueValidator import datetime class MyModel(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: abstract = True class Country(MyModel): name = models.CharField("Название", max_length=200, unique=True) class Meta: ordering = ["name"] verbose_name = "Страна" verbose_name_plural = "Страны" def __str__(self): return self.name class Genre(MyModel): name = models.CharField("Название", max_length=200, unique=True) class Meta: ordering = ["name"] verbose_name = "Жанр" verbose_name_plural = "Жанры" def __str__(self): return self.name class Person(MyModel): name = models.CharField("Имя", max_length=400) origin_name = models.CharField("Имя в оригинале", max_length=400, blank=True, null=True) birthday = models.DateField("Дата рождения", blank=True, null=True, validators=[ MaxValueValidator( limit_value=datetime.date.today) ]) photo = models.ImageField( "Фото", upload_to='photos/', blank=True, null=True) kinopoisk_id = models.PositiveIntegerField( "Kinopoisk ID", blank=True, null=True) def age(self): if not self.birthday: return None today = datetime.date.today() return today.year - self.birthday.year \ - ((today.month, today.day) < (self.birthday.month, self.birthday.day)) class Meta: ordering = ["name"] verbose_name = "Персона" verbose_name_plural = "Персоны" def __str__(self): return self.name class Film(MyModel): name = models.CharField("Имя", max_length=1024) origin_name = models.CharField( "Название (в оригинале)", max_length=1024, blank=True, null=True) slogan = models.CharField("Девиз", max_length=2048, blank=True, null=True) country = models.ForeignKey( Country, on_delete=models.CASCADE, verbose_name="Страна") genres = models.ManyToManyField(Genre, verbose_name="Жанр") director = models.ForeignKey( Person, on_delete=models.CASCADE, verbose_name="Режиссер", related_name="directed_films") length = models.PositiveIntegerField( "Продолжительность", blank=True, null=True) year = models.PositiveIntegerField("Год выпуска", blank=True, null=True, validators=[MinValueValidator( limit_value=1885)]) trailer_url = models.URLField("Трейлер", blank=True, null=True) cover = models.ImageField( "Постер", upload_to='covers/', blank=True, null=True) description = models.TextField("Описание", blank=True, null=True) people = models.ManyToManyField(Person, verbose_name="Актеры") kinopoisk_id = models.PositiveIntegerField( "Kinopoisk ID", blank=True, null=True) class Meta: ordering = ["name"] verbose_name = "Фильм" verbose_name_plural = "Фильмы" def __str__(self): return self.name