在进程重新启动之前,Django Autocomplete Light 不会 return 结果

Django Autocomplete Light doesn't return result until process restart

当我在数据库中添加一条新记录时,它不会出现在自动完成结果中,直到我重新启动 python 进程。我的注册表如下所示。

    import autocomplete_light

    from .models import Article


    autocomplete_light.register(
        Article,

        choices=Article.objects.published(),

        search_fields=['title', '^id', ],

        attrs={
            'placeholder': 'Search by Article Name',
        },
        widget_attrs={
            'class': 'modern-style',
        },
    )

是否缓存了选择选项?

我最终使用 class 作为注册表并设置 request_choices 而不是上述方法。

import autocomplete_light

from apps.abstract.models import PUBLISHED
from .models import Article


class ArticleAutocomplete(autocomplete_light.AutocompleteModelBase):
    attrs = {
        'placeholder': 'Search by Article Title',
    }
    search_fields = ['title', '^id', ]
    widget_attrs = {
        'class': 'modern-style',
    }

    def choices_for_request(self):
        """Returns published articles for the request"""
        self.choices = self.choices.filter(status=PUBLISHED)
        return super(ArticleAutocomplete, self).choices_for_request()

autocomplete_light.register(Article, ArticleAutocomplete)