我正在尝试为我的Django项目中的django-taggit
标签添加搜索过滤器,但是我一直在获取这个AttributeError
。
我将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()
File "C:\Users\loicq\Desktop\Coding\UVERGO_SEARCH\venv\src\search\forms.py", line 19, in search
sqs = self.queryset.filter(ptags__name__in=ptags).distinct()
AttributeError: 'FacetedProductSearchForm' object has no attribute 'queryset'
[02/May/2019 18:38:34] "GET /search/?q=las+vegas&ptags=Solo HTTP/1.1" 500 86568
我的模特:
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
我的自定义表格。py:
from haystack.forms import FacetedSearchForm
from django.db.models.query import QuerySet
class FacetedProductSearchForm(FacetedSearchForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
data = dict(kwargs.get("data", []))
super(FacetedProductSearchForm, self).__init__(*args, **kwargs)
def search(self):
sqs = super(FacetedProductSearchForm, self).search()
ptags = self.request.GET.get('ptags', None)
if ptags:
ptags = ptags.split(',')
sqs = self.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'
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["request"] = self.request
return kwargs
我的祝福:
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)