Create a movies app such as the one described in the Filtering object lists recipe from Chapter 3, Forms and Views. This will have a paginated list view for the movies. For the purposes of this recipe, you can either create a Movie model or a list of dictionaries with the movie data. Every movie will have title, release_year, rank, and rating fields. Release years can range from 1888 through to the current year, and ratings can be any number from 0 to 10, inclusive. The changes will be something such as the following:
# movies/models.py
from datetime import datetime
from django.core.validators import (MaxValueValidator,
MinValueValidator)
from django.db import models
from django.utils.translation import ugettext_lazy as _
# ...
class Movie(models.Model):
# ...
release_year = models.PositiveIntegerField(
_("Release year"),
validators=[
MinValueValidator(1888),
MaxValueValidator(datetime.now().year),
],
default=datetime.now().year)
rating = models.PositiveIntegerField(
_("Rating"),
validators=[
MinValueValidator(0),
MaxValueValidator(10),
])
rank = models.PositiveIntegerField(
unique=True,
blank=False,
null=False,
default=0)
@property
def rating_percentage(self):
"""Convert 0-10 rating into a 0-100 percentage"""
return int(self.rating * 10)
def __str__(self):
return self.title