Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Wagtail:通过模型对多个搜索进行过滤_Django_Wagtail - Fatal编程技术网

Django Wagtail:通过模型对多个搜索进行过滤

Django Wagtail:通过模型对多个搜索进行过滤,django,wagtail,Django,Wagtail,在一个基于的站点中,我有一个ArticlePage模型,它与作者代码片段模型有多对多关系,如下所示: from django.db import models from modelcluster.fields import ParentalKey from wagtail.core.models import Orderable, Page from wagtail.search import index from wagtail.snippets.models import register_

在一个基于的站点中,我有一个
ArticlePage
模型,它与
作者
代码片段模型有多对多关系,如下所示:

from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.core.models import Orderable, Page
from wagtail.search import index
from wagtail.snippets.models import register_snippet

class ArticlePage(Page):
    search_fields = Page.search_fields + [
       index.FilterField('author_id'),
    ]

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

class ArticleAuthorRelationship(Orderable, models.Model):
    author = models.ForeignKey('Author', on_delete=models.CASCADE, related_name='articles')
    page = ParentalKey('ArticlePage', on_delete=models.CASCADE, related_name='authors')
author = Author.objects.get(id=1)

articles = ArticlePage.objects.live() \
                              .filter(authors__author=author) \
                              .search('football')
我希望能够搜索
文章页面
并按特定的
作者
对其进行筛选,我可以这样做:

from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.core.models import Orderable, Page
from wagtail.search import index
from wagtail.snippets.models import register_snippet

class ArticlePage(Page):
    search_fields = Page.search_fields + [
       index.FilterField('author_id'),
    ]

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

class ArticleAuthorRelationship(Orderable, models.Model):
    author = models.ForeignKey('Author', on_delete=models.CASCADE, related_name='articles')
    page = ParentalKey('ArticlePage', on_delete=models.CASCADE, related_name='authors')
author = Author.objects.get(id=1)

articles = ArticlePage.objects.live() \
                              .filter(authors__author=author) \
                              .search('football')
但在dev服务器日志中,我得到了以下警告:

articles.ArticlePage:ArticlePage.search\u字段包含不存在的字段“author\u id”

我的问题是:我做得对吗?它是有效的,但警告让我认为可能有更正确的方法来实现同样的结果


我还尝试过使用
FilterField('authors')
,这会停止警告,但我不知道如何对其进行过滤

正确的方法在

以下是官方文件中共享的示例:

来自wagtail.search导入索引
教材(models.Model,index.index):
...
搜索\u字段=[
index.SearchField('title'),
index.FilterField('published_date'),
index.RelatedFields('作者'[
index.SearchField('name'),
index.FilterField(“出生日期”),
]),
]
因此,在你的情况下,你应该写:

class文章页(第页):
搜索\字段=第页。搜索\字段+[
index.RelatedFields(
“作者”,[index.SearchField(“id”)]
),
]

让我知道它是否有效

我还没有尝试过这个。。但是你试过:
index.FilterField('articles\uu name')
?我想你是指
authors\uu name
?但是没有,
文章名
作者名
作者id
作者名
作者id
都会生成相同的警告。
ArticlePage.objects.live().filter(author\u name='Phil')
,author(字段名)不是作者。谢谢@allcaps,但过滤查询集与添加Wagtail
search\u字段不同。谢谢您的建议,但不幸的是,我收到了类似的警告:
articles.ArticlePage:ArticlePage.search\u字段包含不存在的字段“author”
,很抱歉没有更新我的答案,文档中的DB后端不支持其他字段的索引:有一种解决方法,您可以在模型中创建
新字段
,然后在
保存
方法中将外键保存为该新字段中的字符串,然后在该新字段上进行搜索,(我使用这个小解决方案可以在不更改数据库的情况下进行搜索)我正在使用PostgreSQL后端。我对解决方案很感兴趣。我还编写了我使用的解决方案,它对你有用吗?@balsagoth