Given a models.py:
class Partner(models.Model):
... (fields irrelevant to this example)
class Lecture(models.Model):
... (other fields not relevant to this example)
partner models.ForeignKey(Partner)
I have a ListView for each and a DetailView for each (working fine).
The problem is that on the Partner DetailView page, I have the list of Lectures. In many cases this list can be quite long (e.g., >200), and the stakeholders want it to be paginated. I've had no problems with pagination on the ListView pages (that's easy), but I can't seem to figure out on the Partner's DetailView page how to paginate on their Lecture list.
I was hoping to see in the Django docs code that would look something like:
class PartnerDetailView(DetailView):
model = Partner
paginate_using = Lecture # (or something like self.lecture ?)
paginate_by = 20
whereupon the DetailView would act on the single Partner object, but would (easily) allow pagination from the Lecture FK results.
Is there support for this? Or will it require far more custom view code (perhaps putting a 'page' variable in **kwargs for get_context_data() and creating the subset based on that)?
It just seems like a very common situation under CBVs so I'm puzzled why a search hasn't turned up any examples.
UPDATE: It should have occurred to me that a simple way to do this would be to just add a "page" piece to the url() entry that references the DetailView and use that to create the subset of pagination on the FK objects.
Note that this also might be a workable approach to getting around the "how do you paginate results from a FormView" issue...
Your best bet is probably to subclass ListView rather than DetailView, and override get_queryset
to get the Lectures from the Partner. You can add the Partner object in get_context_data
as well if you need it.
Hello. I would use MultipleObjectMixin and have pagination just like you did in ListView.
from django.views.generic.detail import DetailView
from django.views.generic.list import MultipleObjectMixin
class PartnerDetailView(DetailView,MultipleObjectMixin):
model = Partner
paginate_by = 5
def get_context_data(self, **kwargs):
object_list = Lecture.objects.filter(partner=self.get_object())
context = super(PartnerDetailView, self).get_context_data(object_list=object_list, **kwargs)
return context
Now in your template you can use object_list (all the lectures that are related to this partener) and use pagination.
{% if is_paginated %}
{% include "includes/pagination.html" %}
{% endif %}
where in "includes/pagination.html" you have access to the pagination context(page_obj).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With