0

我正在尝试在我的 django 模型中使用弹性搜索。

目前,如果我title只使用它进行搜索并返回正确的记录,但是当我尝试添加 carparts 过滤器时,它要求我添加index.FilterField('cartpart_id')我所做的但现在它会抛出一个错误

“NoneType”对象没有属性“_meta”

这是我的代码

class IndexPage:
    def get_context(self, request):
        recipes = CarPage.objects.child_of(self).live().public() \
            .select_related('listing_image')
        extra_url_params = ''

        s = get_search_backend()

        filter_name = request.GET.get("title")
        filter_carparts = request.GET.get('carparts')

        if filter_carparts:
            filter_carparts = filter_carparts.split(",")
            filter_carparts = [ int(x) for x in filter_carparts ] #Output = [1,2,3]

        if filter_name and filter_carparts:
            cars = cars.filter(carparts__carpart__in=filter_carparts).distinct()
            cars = s.search(filter_name, recipes)

        elif filter_carparts and not filter_name:
            cars = cars.filter(carparts__carpart__in=filter_carparts).distinct()
            extra_url_params = 'carparts={}'.format()

        elif filter_name and not filter_carparts:
            cars = s.search(filter_name, recipes)

        else:
            cars = cars
        return context



@register_snippet
class CarPart(models.Model):
    title = models.CharField(max_length=50)

    class Meta:
        verbose_name_plural = 'Car Parts'

    def __str__(self):
        return self.title

class CarParts(model.sModel):
    source_page = ParentalKey(
        'home.CarPage', related_name='carparts'
    )

    carpart = models.ForeignKey(
        CarPart,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    panels = [
        SnippetChooserPanel('catpart'),
    ]

    class CarPage(BasePage):
    enter code here
    content_panels = BasePage.content_panels + [
       InlinePanel('carparts', label="Car Part"),
    ]
    def get_context(self, request):
       context = super().get_context(request)

    return context

class CarPage(BasePage):
    title= models.CharField(max_length=20)
    search_fields = BasePage.search_fields + [
        index.SearchField('title'),
        index.FilterField('cartpart_id') <--- issue
    ]
    content_panels = BasePage.content_panels + [
        FieldPanel('title'),
    ]
    def get_context(self, request):
        context = super().get_context(request)
    return context

完整追溯:

Traceback:

File "/home/mycars/code/env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/mycars/code/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/home/mycars/code/env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/core/views.py" in serve
  24.     return page.serve(request, *args, **kwargs)

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/core/models.py" in serve
  721.             self.get_context(request, *args, **kwargs)

File "/home/mycars/code/models.py" in get_context
  171.             cars = s.search(filter_name, cars)

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in search
  370.             partial_match=partial_match,

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in _search
  358.         search_query.check()

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in check
  157.         self._get_filters_from_where_node(self.queryset.query.where, check_only=True)

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in _get_filters_from_where_node
  108.             child_filters = [self._get_filters_from_where_node(child) for child in where_node.children]

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in <listcomp>
  108.             child_filters = [self._get_filters_from_where_node(child) for child in where_node.children]

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in _get_filters_from_where_node
  100.             return self._process_filter(field_attname, lookup, value, check_only=check_only)

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/base.py" in _process_filter
  78.             result = self._process_lookup(field, lookup, value)

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/elasticsearch5.py" in _process_lookup
  20.         column_name = self.mapping.get_field_column_name(field)

File "/home/mycars/code/env/lib/python3.6/site-packages/wagtail/search/backends/elasticsearch2.py" in get_field_column_name
  105.             prefix = definition_model._meta.app_label.lower() + '_' + definition_model.__name__.lower() + '__'

Exception Type: AttributeError at /cars/
Exception Value: 'NoneType' object has no attribute '_meta'
4

0 回答 0