Skip to main content
 首页 » 编程设计

python之主字段名称(文档=真)

2025年05月04日14youxin

Django Haystack docs say :

**Warning** 
When you choose a document=True field, it should be consistently named across all of your SearchIndex classes to avoid confusing the backend. The convention is to name this field text. 
 
There is nothing special about the text field name used in all of the examples. It could be anything; you could call it pink_polka_dot and it won’t matter. It’s simply a convention to call it text. 

但是我不明白这是什么意思。这是他们的示例模型:

导入日期时间 从 haystack 导入索引 from myapp.models import 注意

class NoteIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True, use_template=True) 
    author = indexes.CharField(model_attr='user') 
    pub_date = indexes.DateTimeField(model_attr='pub_date') 
 
    def get_model(self): 
        return Note 
 
    def index_queryset(self, using=None): 
        """Used when the entire index for model is updated.""" 
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now()) 

我引用的文本是指我的模型主字段并说我应该称它为“文本”还是指在 search_indexes.py 中定义的类?

如果是 search_indexes.py 中的类,在上面的示例中它所附加的字段名称在哪里?它没有 model_attr!

text = indexes.CharField(document=True, use_template=True) 

如果对于我的实际应用程序模型,我应该如何重构包含许多应用程序的项目以将其主要文本字段称为“文本”!

请指教。谢谢。

请您参考如下方法:

您的 SearchIndex定义不需要反射(reflect)您的模型定义,它需要将来自不同模型的数据映射到一个通用的搜索文档。

  1. 为什么文本字段需要命名一致?
  2. map 内容的来源是什么? (为什么没有model_attr关键字)

Haystack 文档建议您的 SearchIndex字段应在您的 SearchIndex 中一致命名定义 - 并不是说​​您的模型字段需要命名一致。搜索索引定义和模型定义之间存在主要区别。您不需要也不应该担心模型字段和搜索字段之间的 1-1 映射。

从您的模型中退一步,首先考虑您要搜索的内容。您会通过一个通用的搜索 View 搜索多个不同的模型吗?假设您有两个模型:

class Note(models.Model): 
    title = models.CharField(max_length=40) 
    body = models.TextField() 
 
class Memo(models.Model): 
    subject = models.CharField(max_length=50) 
    content = models.TextField() 
    author = models.ForeignKey(StaffMember) 

我们想要创建一个简单的搜索 View ,它只搜索模型的主要内容以及内容对象的标题或名称(名称、标题、主题等)。

这是一个不好的例子(不要这样做):

class NoteIndex(indexes.SearchIndex, indexes.Indexable): 
    body = indexes.CharField(document=True, use_template=True) 
    title = indexes.CharField(model_attr='title') 
 
    def get_model(self): 
        return Note 
 
class MemoIndex(indexes.SearchIndex, indexes.Indexable): 
    content = indexes.CharField(document=True, use_template=True) 
    subject = indexes.CharField(model_attr='subject') 
 
    def get_model(self): 
        return Memo 

在这个糟糕的例子中,每个搜索索引确实定义了一个主要内容字段和一个内容名称字段(标题或主题)。但是你现在如何搜索它?如果您根据 subject 对内容运行查询你会错过Note内容,同样,如果您查询 body .

更好的例子(这样做):

class NoteIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True, use_template=True) 
    title = indexes.CharField(model_attr='title') 
 
    def get_model(self): 
        return Note 
 
class MemoIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True, use_template=True) 
    title = indexes.CharField(model_attr='subject') 
 
    def get_model(self): 
        return Memo 

请注意,字段名称不一定与模型字段名称匹配。您只需定义来自哪个模型属性的 SearchIndex字段应获取其数据。

您在搜索引擎中搜索文档,而不是在数据库中搜索行,因此 SeachIndex定义将内容从数据库(一个表或对多个表的查询)映射到搜索文档。 SearchIndex定义是一个转换,每个 SearchField按照您的指定转换数据。

关于你关于失踪model_attr的问题,这只是获取内容的一种方式。您还可以从模板呈现文本内容,这就是 text上面的字段(请参阅该字段的 SearchField API documentation)。 model_attr source 适用于简单的字符字段。