Skip to content
Advertisement

How do I get all the commented TV and movie scores in descending order? django

What we want to achieve.

I want to sort the movies according to their star ratings and filter the movies with lower rating than the specified threshold. 2. get the IDs of Movie and TV that are commented in the View, and get the data and score by url one by one. 3. Return it to the html and display the movies and TVs in descending order of score.

Present condition

The tmdb API is used to retrieve movie information. And the model movie and TV have only ID and stars(score). From here, only those with stars are returned to html.

I want to ask a question.

And if score is selected, how can I make it appear in descending score order? data = requests.get (f “https://api.themoviedb.org/3/tv/{tv_id}?api_key={TMDB_API_KEY}&language=en-US”). Now you can get the information of tv.

FieldError at /
Cannot resolve keyword '' into field. Choices are: comment_movie, id, stars
def index(request):

    # Render the template
    sort_by = request.POST.get('sort_by','')
    movie = Movie.objects.order_by(sort_by)
    tv = TV.objects.order_by(sort_by)
    movie_id = movie.GET.get('id')
    tv_id = tv.GET.get('id')
    print(tv_id)
    return render(request, 'Movie/index.html')
def view_movie_detail(request, movie_id):
    
    # It makes little sense you create a movie with just an id attr
    # So I use get_object_or_404 instead
    if not(Movie.objects.filter(id=movie_id)):
        Movie(id = movie_id).save()
    movie = get_object_or_404(Movie, id=movie_id)

    if not(request.user.id == None):
        try:
            comment_movie = Comment_movie.objects.get(user=request.user, movie=movie)
        except Comment_movie.DoesNotExist:
            comment_movie = None
    else:
        comment_movie = None
        
    if request.method == 'POST':
        if request.POST.get('action') == 'delete':
            comment_movie.delete()
            return redirect('view_movie_detail', movie_id=movie_id)
        else:
            form = Comment_movie_CreateForm(request.POST, instance=comment_movie)
            if form.is_valid() and request.POST.get('action') == 'update':
                form.save()
                return redirect('view_movie_detail', movie_id=movie_id)
            elif form.is_valid() and request.POST.get('action') == 'create':
                Comment_movie(
                comment = form.cleaned_data['comment'], 
                user = request.user,
                stars = form.cleaned_data['stars'],
                movie = movie
            ).save()
                return redirect('view_movie_detail', movie_id=movie_id)
    else:
        form = Comment_movie_CreateForm(instance=comment_movie)

    # Put your view logic outside of the conditional expression. 
    # Otherwise your code breaks when the form validates to False
    data = requests.get(f"https://api.themoviedb.org/3/movie/{movie_id}?api_key={TMDB_API_KEY}&language=en-US")
    recommendations = requests.get(f"https://api.themoviedb.org/3/movie/{movie_id}/recommendations?api_key={TMDB_API_KEY}&language=en-US")
    if not(request.user.id == None):
        comments = reversed(Comment_movie.objects.filter(movie_id=movie_id).exclude(user=request.user))
        mycomment = reversed(Comment_movie.objects.filter(movie_id=movie_id,user=request.user))
    else:
        comments = reversed(Comment_movie.objects.filter(movie_id=movie_id))
        mycomment = reversed(Comment_movie.objects.none())
    average = movie.average_stars()
    context = {
        "data": data.json(),
        "recommendations": recommendations.json(),
        "type": "movie",
        "mycomment": mycomment,
        "comments": comments,
        "average" : average,
        "form": form,
        "comment_movie": comment_movie, # NOTE add the comment to context
    }
    return render(request, "Movie/movie_detail.html", context)
class Movie(models.Model):
    id = models.CharField(primary_key=True, editable=False,
                          validators=[alphanumeric],max_length = 9999)
    stars = models.FloatField(
                    blank=False,
                    null=False,
                    default=0, 
                    validators=[MinValueValidator(0.0),
                     MaxValueValidator(10.0)]
                     )
    def get_comments(self):
        return Comment_movie.objects.filter(movie_id=self.id)
    
    def average_stars(self):
        comments = self.get_comments()
        n_comments = comments.count()

        if n_comments:
            self.stars = sum([comment.stars for comment in comments]) / n_comments
        else:
            self.stars = 0
        return self.stars
class TV(models.Model):
    id = models.CharField(primary_key=True, editable=False,
                          validators=[alphanumeric],max_length = 9999)
    stars = models.FloatField(
                    blank=False,
                    null=False,
                    default=0, 
                    validators=[MinValueValidator(0.0),
                     MaxValueValidator(10.0)]
                     )
    def get_comments(self):
        return Comment_tv.objects.filter(tv_id=self.id)
    
    def average_stars(self):
        comments = self.get_comments()
        n_comments = comments.count()

        if n_comments:
            self.stars = sum([comment.stars for comment in comments]) / n_comments
        else:
            self.stars = 0
        return self.stars
          

class Comment_movie(models.Model):
    comment     = models.TextField(max_length=1000)
    stars       = models.FloatField(
                     blank=False,
                     null=False,
                     default=0, 
                     validators=[MinValueValidator(0.0),
                     MaxValueValidator(10.0)]
                  )

    user        = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    movie       = models.ForeignKey(Movie, on_delete=models.CASCADE)
    created_at  = models.DateTimeField(default=datetime.now)
    updated_at  = models.DateTimeField(auto_now=True)
    class Meta:
        unique_together = ('user', 'movie')
        indexes = [
        models.Index(fields=['user', 'movie']),
        ]
    
class Comment_tv(models.Model):
    class Meta:
        unique_together = ('user', 'tv',)
    
    comment     = models.TextField(max_length=1000)
    stars       = models.FloatField(
                     blank=False,
                     null=False,
                     default=0, 
                     validators=[MinValueValidator(0.0),
                     MaxValueValidator(10.0)]
                  )

    user        = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    tv          = models.ForeignKey(TV, on_delete=models.CASCADE)
    created_at  = models.DateTimeField(default=datetime.now)
    updated_at  = models.DateTimeField(auto_now=True)
    class Meta:
        unique_together = ('user', 'tv')
        indexes = [
        models.Index(fields=['user', 'tv']),
        ]

{% extends "base.html" %}
{% block content %}
    <h2>Trending</h2>
    <div class="input-group mb-3">
        <select class="form-select" id="media_type">
          <option selected>All Trending Media Types</option>
          <option value="movie">All Trending Movies(day)</option>
          <option value="tv">All Trending TV Shows(day)</option>
          <option value="person">All Trending Persons(day)</option>
     <option value="score">score</option>     
        </select>
    </div>
    
    <div id="trendings" class="row">

    </div>
    {% load static %}
    <script src="{% static 'js/Movieindex.js' %}"></script>
    
{% endblock %}
fetchTrendingResults("all", "week")

        var mediaType = document.getElementById("media_type")
        mediaType.addEventListener("change", function(event) {
            fetchTrendingResults(mediaType.options[mediaType.selectedIndex].value, "day")
        })

        function fetchTrendingResults(media_type, time_window) {
            var trendingDiv = document.getElementById("trendings")
            trendingDiv.innerHTML = ""

            fetch(`/api/trendings?media_type=${media_type}&time_window=${time_window}`, {
                method: "GET",
                headers: {
                    "Content-Type": "application/json"
                }}
                // todo:movieとTVのIDをもらってこれをURLにFethして映画とTVの情報をそれぞれでスターが高い順に表示する。
                )
            .then(res => res.json())
            .then(data => {
                for (let i=0; i<data.results.length; i++) {
                    var mainDiv = document.createElement("div");
                    mainDiv.setAttribute("class", "card");
                    mainDiv.setAttribute("style", "width: 18rem;");
                    var img = document.createElement("img");
                    img.setAttribute("src", "https://image.tmdb.org/t/p/w200" + data.results[i].poster_path);
                    img.setAttribute("class", "card-img-top");
                    img.setAttribute("alt", "...");
                    var body = document.createElement("div");
                    body.setAttribute("class", "card-body");
                    var title = document.createElement("h5");
                    title.setAttribute("class", "card-title");
                    if (data.results[i].name) {
                        title.innerHTML = data.results[i].name;
                    } else {
                        title.innerHTML = data.results[i].title;
                    }
                    //var text = document.createElement("p");
                    //text.setAttribute("class", "card-text");
                    //text.innerHTML = data.results[i].overview;
                    var link = document.createElement("a");
                    link.setAttribute("href", "/" + data.results[i].media_type + "/" + data.results[i].id + "/");
                    link.setAttribute("class", "btn btn-primary");
                    link.innerHTML = "View Details";
                    body.appendChild(title);
                    //body.appendChild(text);
                    body.appendChild(link);
                    mainDiv.appendChild(img);
                    mainDiv.appendChild(body);
                    document.getElementById("trendings").appendChild(mainDiv);
                }
            })
        }

Advertisement

Answer

You have reversed() which usually reverses the order of a list – there is a better way of doing this with order_by and using - in front of your field to change whether it is in ascending or descending order:

tv = TV.objects.order_by('-stars')
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement