from django.db import models from django.db.models import Avg class Genre(models.Model): name = models.CharField(max_length=100,verbose_name="类型") class Meta: db_table='Genre' verbose_name='电影类型' verbose_name_plural='电影类型' def __str__(self): return self.name class Movie(models.Model): name=models.CharField(max_length=256,verbose_name="电影名") imdb_id = models.IntegerField(verbose_name="imdb_id") time=models.CharField(max_length=256,blank=True,verbose_name="时长") genre =models.ManyToManyField(Genre,verbose_name="类型") release_time=models.CharField(max_length=256,blank=True,verbose_name="发行时间") intro=models.TextField(blank=True,verbose_name="简介") director=models.CharField(max_length=256,blank=True,verbose_name="导演") actor=models.CharField(max_length=256,blank=True,verbose_name="演员") writers=models.CharField(max_length=256,blank=True,verbose_name="编剧") movie_similarity=models.ManyToManyField('self',through="Movie_similarity",symmetrical=False,verbose_name="相似电影") class Meta: db_table='Movie' verbose_name='电影信息' verbose_name_plural='电影信息' def __str__(self): return f"" def get_score(self): result_dct=self.movie_rating_set.aaggregate(Avg('score')) try: result=round(result_dct['score__avg'],1) except TypeError: return 0 else: return result def get_user_score(self,user): return self.movie_rating_set.filter(user=user).values('score') def get_score_int_range(self): return range(int(self.get_score())) def get_genre(self): genre_dct=self.genre.all().values('name') genre_lst=[] for dct in genre_dct.values(): genre_lst.append(dct['name']) return genre_lst def get_similarity(self,k=5): similarity_movies=self.movie_similarity.all()[:k] return similarity_movies class Movie_similarity(models.Model): movie_source=models.ForeignKey(Movie,related_name='movie_source',on_delete=models.CASCADE) movie_target=models.ForeignKey(Movie,related_name='movie_target',on_delete=models.CASCADE) similarity=models.FloatField(verbose_name="相似度") class Meta: verbose_name='电影相似度' verbose_name_plural='电影相似度' class User(models.Model): name=models.CharField(max_length=128,unique=True,verbose_name="用户名") password=models.CharField(max_length=256,verbose_name="密码") email=models.EmailField(unique=True,verbose_name="邮箱") rating_movies=models.ManyToManyField(Movie,through="Movie_rating") def __str__(self): return self.name class Meta: db_table='Usser' verbose_name='用户信息' verbose_name_plural='用户信息' class Movie_rating(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, unique=False, verbose_name="用户") movie=models.ForeignKey(Movie,on_delete=models.CASCADE,unique=False,verbose_name="电影") score=models.FloatField(verbose_name="评分") comment=models.TextField(blank=True,verbose_name="评论") class Meta: db_table='Movie_rating' verbose_name='电影评分信息' verbose_name_plural='电影评分信息' class Movie_hot(models.Model): movie = models.ForeignKey(Movie, on_delete=models.CASCADE, verbose_name="电影名") rating_number = models.IntegerField(verbose_name="评分人数") class Meta: db_table='Movie_hot' verbose_name='最热电影' verbose_name_plural='最热电影'