Skip to content
Advertisement

How do I sort the movies according to their star ratings and filter the movies with lower rating than the specified threshold

I want to sort the movies according to their star ratings and filter the movies with lower rating than the specified threshold. I want to sort movie and TV information in descending order by stars, search by each ID, and display data.json format and socre. However, I get a’module’ object does not support item assignment error. context [“movie”] = in movie_list

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
          
def Score_by(request):
    movies = Movie.objects.order_by('-stars')
    movie_list= []
    if movies.exists():
        for obj in movies:
            data = requests.get(f"https://api.themoviedb.org/3/movie/{obj.id}?api_key={TMDB_API_KEY}&language=en-US")
            movie_list.append(data.json())
        context["movie"] = movie_list
    tv = TV.objects.order_by('-stars')
    tv_list = []
    if tv.exists():
        for obj in tv:
            data = requests.get(f"https://api.themoviedb.org/3/tv/{obj.id}?api_key={TMDB_API_KEY}&language=en-US")
            tv_list.append(data.json())
        context["tv"] = tv_list
    return render(request, 'Movie/score_by.html', context)
<div class="row">

    {% for m in movie.results %}
        <div class="card" style="width: 18rem;">
            <img src="https://image.tmdb.org/t/p/w200{{ m.poster_path }}" class="card-img-top" alt="...">
            <div class="card-body">
            {% if not m.name %}
                <h5 class="card-title">{{ m.title }}</h5>
            {% else %}
                <h5 class="card-title">{{ m.name }}</h5>
            {% endif %}
            <p class="card-text">{{ m.overview }}</p>
            <a href="/{{ type }}/{{ m.id }}/" class="btn btn-primary">View Details</a>
            </div>
        </div>
    {% endfor %}
</div>
<h1>TV Score_by</h1>
<div class="row">
    {% for m in tv.results %}
        <div class="card" style="width: 18rem;">
            <img src="https://image.tmdb.org/t/p/w200{{ m.poster_path }}" class="card-img-top" alt="...">
            <div class="card-body">
            {% if not m.name %}
                <h5 class="card-title">{{ m.title }}</h5>
            {% else %}
                <h5 class="card-title">{{ m.name }}</h5>
            {% endif %}
            <p class="card-text">{{ m.overview }}</p>
            <a href="/{{ type }}/{{ m.id }}/" class="btn btn-primary">View Details</a>
            </div>
        </div>
    {% endfor %}
</div>

Advertisement

Answer

If I understand correctly, you should have imported context as a module. If so then the error arises because you are essentially trying to index a module in

context["movie"]

this is not supported in python. Say you are trying to store the movies in a ‘movies’ dictionary in the module, then what you should do is:

import context
...
context.movies["movie"] = movie_list

But I think the module would probably have a method returning the dictionary you need, then you can use:

import context
...
movies = context.some_method()
movies["movie"] = movie_list

Same for the tv list.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement