[Answered ]-How to query a Django model (table) and add two related fields from another model (table)? – annotate – left outer join

1👍

Simple is better than complex

def index(request, movie_id):
    rating = Rating.objects.filter(movie_id=movie_id).order_by('-score').first()
    return HttpResponse(rating)

The problem with this solution is that two users could have added the same maximum value, and that is the reason why you should not and cannot use the get method

If you need to access the movie or user object from the template, then you must:

<div class="user">{{ rating.user.get_full_name }}</div>
<div class="movie">{{ rating.movie.title }}</div>
<div class="score">{{ rating.score }}</div>

0👍

You Can get max rating of a particular movie by Rating model

def index(request, movie_id):
    movie = Rating.objects.filter(movie_id=movie_id).order_by('-rating')
    return HttpResponse(movie.first())

Leave a comment