[Python] Django - pagination for inline models

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I realize this is probably a beginner level error, but I'm out of ideas. I need to add pagination to Inline model for admin page. I'm using Django 1.8.4 ( yup, I know it's really old ) and python 3.6.15.

    Inside admin.py:

    class ArticleInline(GrappelliSortableHiddenMixin, admin.TabularInline):
    model = ArticleSection.articles.through
    raw_id_fields = ("article",)
    related_lookup_fields = {
    'fk':['article'],
    }
    extra = 1

    class ArticleSectionsAdmin(reversion.VersionAdmin):
    list_display = ('name','slug','template_file_name')
    inlines = [ArticleInline]

    admin.site.register(ArticleSection,ArticleSectionsAdmin)


    Inside models.py:

    class ArticleSection(Section):
    articles = models.ManyToManyField(Article, verbose_name=_("article"), through="M2MSectionArticle", related_name="sections")
    limit = models.PositiveSmallIntegerField(default=10)

    class Meta:
    db_table = 'sections_article_section'
    verbose_name = _("article section")
    verbose_name_plural = _("articles sections")

    def content(self, request):
    query = Q(m2msectionarticle__visible_to__isnull=True) & Q(m2msectionarticle__visible_from__isnull=True)
    query.add(Q(m2msectionarticle__visible_to__gte=timezone.now(), m2msectionarticle__visible_from__lte=timezone.now()), Q.OR)

    limit = self.limit_override if hasattr(self,
    'limit_override') and self.limit_override is not None else self.limit
    return self.articles.filter(query).published().prefetch_related('images').order_by('m2msectionarticle__position')[:limit]

    class M2MSectionArticle(models.Model):
    section = models.ForeignKey(ArticleSection, related_name='section_articles')
    article = models.ForeignKey(Article, verbose_name=_('article'))
    position = models.PositiveSmallIntegerField(_("position"), default=0)
    visible_from = models.DateTimeField("Widoczne od", null=True, blank=True)
    visible_to = models.DateTimeField("Widoczne do", null=True, blank=True)

    class Meta:
    db_table = 'sections_section_articles'
    ordering = ["position"]


    I found django-admin-inline-paginator and it seems to work for everyone else, but I get "Function has keyword-only parameters or annotations, use getfullargspec() API which can support them" when I use TabularInlinePaginated instead of admin.TabularInline.

    from django_admin_inline_paginator.admin import TabularInlinePaginated

    class ArticleInline(GrappelliSortableHiddenMixin, TabularInlinePaginated):
    model = ArticleSection.articles.through
    raw_id_fields = ("article",)
    related_lookup_fields = {
    'fk':['article'],
    }
    extra = 1
    per_page = 10


    print(inspect.getfullargspec(TabularInlinePaginated)) returns :

    FullArgSpec(args=['self', 'parent_model', 'admin_site'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})


    but I still dont know what to do.

    Continue reading...

Compartilhe esta Página