Wagtail:通过外键过滤页面
Wagtail: Filter Pages by a ForeignKey
使用 Wagtail 我想获得 Page
的 QuerySet,其特定子类具有 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='+'
)
那么,我如何获得所有 Page
的查询集,其关联的 ArticlePage
具有 Organisation
和 id
的 1
?
ArticlePage.objects.filter(organisation__id=1)
这将为您提供 ArticlePage
个对象的查询集,这通常优于 Page
个对象的查询集,因为它将为您提供 Page 的所有功能以及任何其他字段和在 ArticlePage 上定义的方法。如果出于某种原因你需要它们成为基本的 Page
对象,你可以使用:
Page.objects.filter(articlepage__organisation__id=1)
使用 Wagtail 我想获得 Page
的 QuerySet,其特定子类具有 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='+'
)
那么,我如何获得所有 Page
的查询集,其关联的 ArticlePage
具有 Organisation
和 id
的 1
?
ArticlePage.objects.filter(organisation__id=1)
这将为您提供 ArticlePage
个对象的查询集,这通常优于 Page
个对象的查询集,因为它将为您提供 Page 的所有功能以及任何其他字段和在 ArticlePage 上定义的方法。如果出于某种原因你需要它们成为基本的 Page
对象,你可以使用:
Page.objects.filter(articlepage__organisation__id=1)