Eg3-git / wad2project

0 stars 0 forks source link

Database movie table. #5

Closed FilipVyrostko closed 3 years ago

FilipVyrostko commented 3 years ago

I think we need 3 more tables in the database for movie model. This is the code;

Cover picture for the movie

movie_picture = models.ImageField(upload_to="movie_images", blank=True)

# Time when the movie was uploaded to the website required for index page to display most recently added movies
upload_date = models.DateField(default=datetime.date.today())

# Producer object, the owner of the movie
producer_object = models.ForeignKey(User, on_delete=models.CASCADE)
FilipVyrostko commented 3 years ago

ugh why it made my comment a title

Tnick0 commented 3 years ago

Ok, I will add the first two. The producer is already referenced with a foreign key though, I don't see why a separate producer object is necessary

FilipVyrostko commented 3 years ago

also need to add this

average_rating = models.IntegerField(default=0)
num_of_ratings = models.IntegerField(default=0)
FilipVyrostko commented 3 years ago

Oh yeah did not notice the producer sorry

Tnick0 commented 3 years ago

I'm not sure if adding these fields is a good idea. I believe it is a better approach to obtain these values with the following query: movies = Movie.objects.annotate(avg_rating=Avg('rating__rating').annotate(num_ratings=Count('rating') Now, movies is a list with all stored movies with the added attributes avg_rating and num_ratings, which can be accessed in the following way: movies[i].num_ratings and movies[i].avg_rating. Finally, this list can be sorted by the new avg_rating attribute as follows: movies.order_by('-avg_rating')

FilipVyrostko commented 3 years ago

Oh thats cool, I did not know about this. I will use this then