<reponame>grand-roman/api_for_docker
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from users.models import User


class Category(models.Model):
    """ Модель категорий произведений """
    name = models.CharField(max_length=50)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return self.name


class Genre(models.Model):
    """ Модель жанра """
    name = models.CharField(max_length=50)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return self.name


class Title(models.Model):
    """ Модель произведения """
    name = models.CharField(max_length=150)
    year = models.IntegerField()
    description = models.TextField(max_length=500, blank=True, null=True)
    category = models.ForeignKey(
        Category,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
        related_name='title'
    )
    genre = models.ManyToManyField(
        Genre,
        through='GenreTitle'
    )

    def __str__(self):
        return self.name


class GenreTitle(models.Model):
    """ Модель для отношений жанр-произведение """
    title_id = models.ForeignKey(
        Title,
        on_delete=models.CASCADE,
        related_name='title_genre_id',
    )
    genre_id = models.ForeignKey(
        Genre,
        on_delete=models.CASCADE,
        related_name='genre_title_id'
    )


class Review(models.Model):
    """ Модель отзыва """
    text = models.TextField()
    pub_date = models.DateTimeField('Дата публикации', auto_now_add=True)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='reviews')
    score = models.IntegerField(validators=[
        MaxValueValidator(10),
        MinValueValidator(1)
    ])
    title = models.ForeignKey(
        Title, on_delete=models.CASCADE, related_name='reviews')

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=('author', 'title'),
                name='unique_reviews'
            )
        ]
        unique_together = ('author', 'title')

    def __str__(self):
        """ Строковое представление объекта в поле text """
        return self.text


class Comment(models.Model):
    """ Модель коммента """
    text = models.TextField()
    pub_date = models.DateTimeField('Дата публикации', auto_now_add=True)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='comments')
    review = models.ForeignKey(
        Review, on_delete=models.CASCADE, related_name='comments')

    def __str__(self):
        """ Строковое представление объекта в поле text """
        return self.text

from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from users.models import User


class Category(models.Model):
    """ Модель категорий произведений """
    name = models.CharField(max_length=50)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return self.name


class Genre(models.Model):
    """ Модель жанра """
    name = models.CharField(max_length=50)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return self.name


class Title(models.Model):
    """ Модель произведения """
    name = models.CharField(max_length=150)
    year = models.IntegerField()
    description = models.TextField(max_length=500, blank=True, null=True)
    category = models.ForeignKey(
        Category,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
        related_name='title'
    )
    genre = models.ManyToManyField(
        Genre,
        through='GenreTitle'
    )

    def __str__(self):
        return self.name


class GenreTitle(models.Model):
    """ Модель для отношений жанр-произведение """
    title_id = models.ForeignKey(
        Title,
        on_delete=models.CASCADE,
        related_name='title_genre_id',
    )
    genre_id = models.ForeignKey(
        Genre,
        on_delete=models.CASCADE,
        related_name='genre_title_id'
    )


class Review(models.Model):
    """ Модель отзыва """
    text = models.TextField()
    pub_date = models.DateTimeField('Дата публикации', auto_now_add=True)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='reviews')
    score = models.IntegerField(validators=[
        MaxValueValidator(10),
        MinValueValidator(1)
    ])
    title = models.ForeignKey(
        Title, on_delete=models.CASCADE, related_name='reviews')

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=('author', 'title'),
                name='unique_reviews'
            )
        ]
        unique_together = ('author', 'title')

    def __str__(self):
        """ Строковое представление объекта в поле text """
        return self.text


class Comment(models.Model):
    """ Модель коммента """
    text = models.TextField()
    pub_date = models.DateTimeField('Дата публикации', auto_now_add=True)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='comments')
    review = models.ForeignKey(
        Review, on_delete=models.CASCADE, related_name='comments')

    def __str__(self):
        """ Строковое представление объекта в поле text """
        return self.text
Модель категорий произведений Модель жанра Модель произведения Модель для отношений жанр-произведение Модель отзыва Строковое представление объекта в поле text Модель коммента Строковое представление объекта в поле text