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...
MultipleObjectMixin with DetailView
Hello. I would use MultipleObjectMixin and have pagination just like you did in ListView.
Now in your template you can use object_list (all the lectures that are related to this partener) and use pagination.
where in "includes/pagination.html" you have access to the pagination context(page_obj).
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 inget_context_data
as well if you need it.To extend @MsCheikh answer i will provide version without MultipleObjectMixin. Sample of my
So later i include standard Django pagination template (copied from docs) and iterate over related_activities