/ search / search()处的TypeError缺少1个必需的位置参数:“ request”

时间:2019-05-02 19:43:35

标签: python django django-haystack

我正在尝试在Django项目中为我的django-taggit标签添加搜索过滤器,但我一直得到一个TypeError

我将Django-Haystack和Elasticsearch用于搜索功能,而这次我正尝试使用haystack Facets添加过滤器。

完整输出:

Internal Server Error: /search/
Traceback (most recent call last):
  File "C:\Users\loicq\desktop\coding\uvergo_search\venv\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\loicq\desktop\coding\uvergo_search\venv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\loicq\desktop\coding\uvergo_search\venv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\loicq\desktop\coding\uvergo_search\venv\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\loicq\desktop\coding\uvergo_search\venv\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\loicq\desktop\coding\uvergo_search\venv\lib\site-packages\haystack\generic_views.py", line 123, in get
    return self.form_valid(form)
  File "C:\Users\loicq\desktop\coding\uvergo_search\venv\lib\site-packages\haystack\generic_views.py", line 76, in form_valid
    self.queryset = form.search()
TypeError: search() missing 1 required positional argument: 'request'
[02/May/2019 15:29:31] "GET /search/?q=las+vegas&Ptags=Solo HTTP/1.1" 500 82872

我的模特:

class Product(models.Model):
    title = models.CharField(max_length=255, default='')
    slug = models.SlugField(null=True, blank=True, unique=True, max_length=255, default='')
    description = models.TextField(default='')

    ptags = TaggableManager()

    image = models.ImageField(default='')
    timestamp = models.DateTimeField(auto_now=True)

    def _ptags(self):
        return [t.name for t in self.ptags.all()]

    def get_absolute_url(self):
        return reverse('product',
                       kwargs={'slug': self.slug})


    def __str__(self):
        return self.title

我的自定义forms.py

from haystack.forms import FacetedSearchForm
from django.db.models.query import QuerySet


class FacetedProductSearchForm(FacetedSearchForm):

    def __init__(self, *args, **kwargs):
        data = dict(kwargs.get("data", []))
        super(FacetedProductSearchForm, self).__init__(*args, **kwargs)

    def search(self, request):
        sqs = super(FacetedProductSearchForm, self).search()

        ptags = request.query_params.get('ptags', None)

        if ptags:
            ptags = ptags.split(',')
            sqs = queryset.filter(ptags__name__in=ptags).distinct()

        return sqs

我将函数从forms.py传递到这样的视图:

class FacetedSearchView(BaseFacetedSearchView):

    form_class = FacetedProductSearchForm
    facet_fields = ['ptags']
    template_name = 'search_result.html'
    paginate_by = 6
    context_object_name = 'object_list'
from django.conf.urls import url
from django.contrib import admin
from search.views import HomeView, ProductView, FacetedSearchView, autocomplete
from .settings import MEDIA_ROOT, MEDIA_URL
from django.conf.urls.static import static

urlpatterns = [
    url(r'^$', HomeView.as_view()),
    url(r'^admin/', admin.site.urls),
    url(r'^product/(?P<slug>[\w-]+)/$', ProductView.as_view(), name='product'),
    url(r'^search/autocomplete/$', autocomplete),
    url(r'^search/', FacetedSearchView.as_view(), name='haystack_search'),

] + static(MEDIA_URL, document_root=MEDIA_ROOT)

1 个答案:

答案 0 :(得分:0)

您已经向request方法中添加了一个search参数,但是Haystack并没有期望该参数,并且不知道将其传递。

您需要采取一些不同的方法。您需要通过视图的get_form_kwargs方法将请求传递给表单实例化,将其存储为实例属性,然后从搜索方法中获取。

class FacetedSearchView(BaseFacetedSearchView):
    ..
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["request"] = self.request
        return kwargs

class FacetedProductSearchForm(FacetedSearchForm):
    def __init__(self, *args, **kwargs):
        self request = kwargs.pop("request", None)
        ...

    def search(self):
        ...
        ptags = self.request.GET.get('ptags', None)