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>
- [Answered ]-Can I use a ModelViewSet for POST and GET of a parent of self in a model in Django REST?
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())
- [Answered ]-Bulk replace with regular expressions in Python
- [Answered ]-Django URL logical OR misunderstood
- [Answered ]-How to execute code on save in Django User model?
- [Answered ]-Models in django. is many to many the best option?
Source:stackexchange.com