How can I paginate the attributes from a model?

I have a single for each series, but I want to paginate inside this page. I want to paginate the episodes because some series have more than 100 episodes and it is too much for a single page.

class SerieDetailedView(DetailView):
    template_name = "tailwind/series_detail.html"
    model = Serie
    slug_url_kwarg = "serie_name"
    slug_field = "name"
    context_object_name = "project"

    def get_object(self, queryset=None):

        return Serie.objects.get(name=self.kwargs["serie_name"])

In this case, I have this as my model:

class Serie(models.Model):

    name = models.CharField(max_length=150, unique=True)
    finished = models.BooleanField(default=False)
    episodes = ArrayField(base_field=models.CharField(max_length=100), default=list)
    published_at = models.DateTimeField(auto_now_add=True)

I’m just getting how to paginate all the series, but it’s different, I want to paginate a thing that is inside the series.

I’ve tried to do it, but it has not worked because is a single page:

class SerieDetailedView(DetailView):
    template_name = "tailwind/series_detail.html"
    model = Serie
    slug_url_kwarg = "serie_name"
    slug_field = "name"
    context_object_name = "project"
        paginate_by = 10

    def get_object(self, queryset=None):

        return Serie.objects.get(name=self.kwargs["serie_name"]).episodes.order_by('-published_at')

I think you did not understand the concept of relationnal database. In your case, you should split your data into two distinct tables :

class Serie(models.Model):
    name = models.CharField(max_length=150, unique=True)
    finished = models.BooleanField(default=False)
    published_at = models.DateTimeField(auto_now_add=True)

class Episode(models.Model):
    serie = models.ForeignKey(Serie, related_name="episodes", on_delete=models.CASCADE)
    # you can add more data here, like name of the episode, duration, airing date, etc.

Then, in your DetailView, you write :

from django.core.paginator import Paginator

class SerieDetailedView(DetailView):
    template_name = "tailwind/series_detail.html"
    model = Serie
    slug_url_kwarg = "serie_name"
    slug_field = "name"
    context_object_name = "project"

    def get_context_data(self, **kwargs):
        data = super().get_context_data(**kwargs)
        episodes = self.objects.episodes.all()
        data["episodes"] = Paginator(episodes, 10).page(1)
        return data
Back to Top