g:通过ForeignKey筛选页面

Phi*_*ord 0 django wagtail

我想使用Wagtail获得的QuerySet,Page其特定子类具有的特定ForeignKey Snippet

from django.db import models
from wagtail.core.models import Page
from wagtail.snippets.models import register_snippet

@register_snippet
class Organization(models.Model):
    name = models.CharField(max_length=255, blank=False)

class ArticlePage(Page):
    organization = models.ForeignKey(
        'Organization',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )
Run Code Online (Sandbox Code Playgroud)

所以,我怎么会得到所有的查询集Page,其相连的S ArticlePage有一个Organisationid1

gas*_*man 5

ArticlePage.objects.filter(organisation__id=1)
Run Code Online (Sandbox Code Playgroud)

这将为您提供ArticlePage对象的查询集,通常比对象的查询集更可取,Page因为它将为您提供Page的所有功能以及在ArticlePage上定义的任何其他字段和方法。如果出于某种原因需要它们作为基本Page对象,则可以使用:

Page.objects.filter(articlepage__organisation__id=1)
Run Code Online (Sandbox Code Playgroud)