etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.2k stars 130 forks source link

Unable to search across related models in Django 1.10 and beyond via middleware #193

Closed Xelinor closed 7 years ago

Xelinor commented 7 years ago

Django upgraded how it's middleware functions, so the search across related models middleware no longer functions and results in an error and will not let pages load.

https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-middleware

Xelinor commented 7 years ago

So after playing with it a bit, I'm able to get the error to clear by editing middleware.py within Watson like so:

"""Middleware used by django-watson."""

from __future__ import unicode_literals

from django.core.exceptions import ImproperlyConfigured

from watson.search import search_context_manager

from django.utils.deprecation import MiddlewareMixin

WATSON_MIDDLEWARE_FLAG = "watson.search_context_middleware_active"

class SearchContextMiddleware(MiddlewareMixin):

    """Wraps the entire request in a search context."""

    def process_request(self, request):
        """Starts a new search context."""
        if request.META.get(WATSON_MIDDLEWARE_FLAG, False):
            raise ImproperlyConfigured("SearchContextMiddleware can only be included in MIDDLEWARE_CLASSES once.")
        request.META[WATSON_MIDDLEWARE_FLAG] = True
        search_context_manager.start()

    def _close_search_context(self, request):
        """Closes the search context."""
        if request.META.get(WATSON_MIDDLEWARE_FLAG, False):
            del request.META[WATSON_MIDDLEWARE_FLAG]
            search_context_manager.end()

    def process_response(self, request, response):
        """Closes the search context."""
        self._close_search_context(request)
        return response

    def process_exception(self, request, exception):
        """Closes the search context."""
        search_context_manager.invalidate()
        self._close_search_context(request)

However this does not actually seem to solve the issue I'm having where my manytomany relations don't seem to be included in the search. Am I doing something wrong or is it just not supported?

etianen commented 7 years ago

What original error did you get? What change did you make to the middleware?

Xelinor commented 7 years ago

The error was raised when I added the existing [middleware classes] to the [middleware] setting in 1.10. I didn't save it at the time because I was (at that point) just trying to get it to work. If I have time later i'll go back and recreate it...

as for the change, all I did was import 'middlewaremixin' from django.utils.deprecation import the MiddlewareMixin class and have the SearchContextMiddleware inherit that.

class SearchContextMiddleware(MiddlewareMixin):

That clears the error, but it doesn't actually make the middleware function as far as I can tell, so it's not a fix...although I'm trying to use a manytomany relation...

etianen commented 7 years ago

The middleware ensures that the search indices are updated correctly on model save. It'll only apply to models saved after the middleware is added. You should run ./manage.py buildwatson to recreate the indices for all current models.

Can I have a pull request that fixes the middleware?

On Mon, 30 Jan 2017 at 18:28 Xelinor notifications@github.com wrote:

The error was raised when I added the existing [middleware classes] to the [middleware] setting in 1.10. I didn't save it at the time because I was (at that point) just trying to get it to work. If I have time later i'll go back and recreate it...

as for the change, all I did was import 'middlewaremixin' from django.utils.deprecation import the MiddlewareMixin class and have the SearchContextMiddleware inherit that.

(class SearchContextMiddleware(MiddlewareMixin):

That clears the error, but it doesn't actually make the middleware function as far as I can tell, so it's not a fix...although I'm trying to use a manytomany relation...

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/etianen/django-watson/issues/193#issuecomment-276147622, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJFCGDIIOjtAPfuG30HQE7FFdFp4yjyks5rXivbgaJpZM4Ltxfs .

blodone commented 7 years ago

had the same problem, i created a pull request: https://github.com/etianen/django-watson/pull/195

etianen commented 7 years ago

Fixed in 1.3.1