来自 Django DetailView 的分页 [英] Pagination from a Django DetailView

查看:19
本文介绍了来自 Django DetailView 的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个models.py:

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)

我每个都有一个 ListView,每个都有一个 DetailView(工作正常).

I have a ListView for each and a DetailView for each (working fine).

问题是在 Partner DetailView 页面上,我有讲座列表.在许多情况下,这个列表可能很长(例如,> 200),并且利益相关者希望它被分页.我在 ListView 页面上的分页没有问题(这很简单),但我似乎无法在合作伙伴的 DetailView 页面上弄清楚如何在他们的讲座列表上进行分页.

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.

我希望在 Django 文档中看到如下所示的代码:

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

因此,DetailView 将作用于单个 Partner 对象,但会(轻松)允许从 Lecture FK 结果中进行分页.

whereupon the DetailView would act on the single Partner object, but would (easily) allow pagination from the Lecture FK results.

有支持吗?或者它是否需要更多的自定义视图代码(也许在 **kwargs 中为 get_context_data() 放置一个页面"变量并基于它创建子集)?

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)?

这似乎是 CBV 下非常常见的情况,所以我很困惑为什么搜索没有找到任何示例.

It just seems like a very common situation under CBVs so I'm puzzled why a search hasn't turned up any examples.

更新:我应该想到一个简单的方法是将一个页面"片段添加到引用 DetailView 的 url() 条目中,并使用它在 FK 上创建分页子集对象.

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.

请注意,这也可能是解决如何对 FormView 的结果进行分页"问题的可行方法...

Note that this also might be a workable approach to getting around the "how do you paginate results from a FormView" issue...

推荐答案

最好的办法可能是将 ListView 子类化而不是 DetailView,并覆盖 get_queryset 以从合作伙伴那里获取讲座.如果需要,您也可以在 get_context_data 中添加 Partner 对象.

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.

这篇关于来自 Django DetailView 的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆