Advertisement
go6odn28

top_actor

Jul 26th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. def get_top_actor():
  2.     top_actor = (Actor.objects.prefetch_related('starring_actor_movies')
  3.                  .annotate(num_movies=Count('starring_actor_movies'),
  4.                            average_rating=Avg('starring_actor_movies__rating'))
  5.                  .order_by('-num_movies', 'full_name')).first()
  6.  
  7.     if not top_actor or top_actor.num_movies == 0:
  8.         return ''
  9.  
  10.     all_movies = ', '.join(m.title for m in top_actor.starring_actor_movies.all())
  11.  
  12.     return (f"Top Actor: {top_actor.full_name}, starring in movies: {all_movies}, "
  13.             f"movies average rating: {top_actor.average_rating:.1f}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement