gpt4 book ai didi

python - django admin中内联模型的分页器

转载 作者:IT老高 更新时间:2023-10-28 20:29:17 28 4
gpt4 key购买 nike

我有这个简单的 django 模型,其中包含一个传感器和特定传感器的值。每个日射强度计的值数很高(> 30k)。是否可以按特定日期对 PyranometerValues 进行分页,或者将分页器应用于管理内联 View ?

class Pyranometer(models.Model):
name = models.CharField(max_length=75)

class PyranometerValues(models.Model):
timestamp = models.DateTimeField()
value = models.DecimalField(max_digits=10,decimal_places=6)
sensor = models.ForeignKey('Pyranometer')

最佳答案

如果有人需要,我发现 this comment 中的分页 TabularInline 子类的这种很好的实现(虽然被描述为“相当老套”)的 a django-suit问题。

对于 Django 1.6,它需要更改模板并继承此 PaginationInline 类:

from django.contrib import admin
from django.contrib.admin.views.main import ChangeList
from django.core.paginator import EmptyPage, InvalidPage, Paginator

class InlineChangeList(object):
can_show_all = True
multi_page = True
get_query_string = ChangeList.__dict__['get_query_string']

def __init__(self, request, page_num, paginator):
self.show_all = 'all' in request.GET
self.page_num = page_num
self.paginator = paginator
self.result_count = paginator.count
self.params = dict(request.GET.items())


class PaginationInline(admin.TabularInline):
template = 'admin/edit_inline/tabular_paginated.html'
per_page = 20

def get_formset(self, request, obj=None, **kwargs):
formset_class = super(PaginationInline, self).get_formset(
request, obj, **kwargs)
class PaginationFormSet(formset_class):
def __init__(self, *args, **kwargs):
super(PaginationFormSet, self).__init__(*args, **kwargs)

qs = self.queryset
paginator = Paginator(qs, self.per_page)
try:
page_num = int(request.GET.get('p', '0'))
except ValueError:
page_num = 0

try:
page = paginator.page(page_num + 1)
except (EmptyPage, InvalidPage):
page = paginator.page(paginator.num_pages)

self.cl = InlineChangeList(request, page_num, paginator)
self.paginator = paginator

if self.cl.show_all:
self._queryset = qs
else:
self._queryset = page.object_list

PaginationFormSet.per_page = self.per_page
return PaginationFormSet

关于python - django admin中内联模型的分页器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6664806/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com