Advertisement
go6odn28

models_1

Jul 26th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. from django.core.validators import MinLengthValidator, MinValueValidator, MaxValueValidator
  2. from django.db import models
  3.  
  4. from main_app.models_mixins import IsAwardedMixin, LastUpdatedMixin
  5. from main_app.custom_models_managers import DirectorManager
  6.  
  7.  
  8. class BasePerson(models.Model):
  9.     full_name = models.CharField(max_length=120, validators=[MinLengthValidator(2)])
  10.     birth_date = models.DateField(default="1900-01-01")
  11.     nationality = models.CharField(max_length=50, default="Unknown")
  12.  
  13.     class Meta:
  14.         abstract = True
  15.  
  16.     def __str__(self):
  17.         return self.full_name
  18.  
  19.  
  20. class Director(BasePerson):
  21.     years_of_experience = models.SmallIntegerField(default=0, validators=[MinValueValidator(0)])
  22.  
  23.     objects = DirectorManager()
  24.  
  25.  
  26. class Actor(BasePerson, IsAwardedMixin, LastUpdatedMixin):
  27.     pass
  28.  
  29.  
  30. class Movie(IsAwardedMixin, LastUpdatedMixin):
  31.     GENRE_CHOICES = (
  32.         ("Action", "Action"),
  33.         ("Comedy", "Comedy"),
  34.         ("Drama", "Drama"),
  35.         ("Other", "Other"),
  36.     )
  37.  
  38.     title = models.CharField(max_length=150, validators=[MinLengthValidator(5)])
  39.     release_date = models.DateField()
  40.     storyline = models.TextField(null=True, blank=True)
  41.     genre = models.CharField(choices=GENRE_CHOICES,
  42.                              max_length=6,
  43.                              default="Other")
  44.  
  45.     rating = models.DecimalField(max_digits=3, decimal_places=1,
  46.                                  validators=[MinValueValidator(0.0), MaxValueValidator(10.0)],
  47.                                  default=0)
  48.  
  49.     is_classic = models.BooleanField(default=False)
  50.     director = models.ForeignKey(Director, on_delete=models.CASCADE,
  51.                                  related_name="director_movies")
  52.  
  53.     starring_actor = models.ForeignKey(Actor, on_delete=models.SET_NULL,
  54.                                        related_name="starring_actor_movies",
  55.                                        null=True, blank=True)
  56.  
  57.     actors = models.ManyToManyField(Actor,
  58.                                     related_name="actor_movies")
  59.  
  60.     def __str__(self):
  61.         return self.title
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement