7π
I wanted to do something similar so I created an app named search
search/models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
class SearchTerm(models.Model):
query = models.CharField(
verbose_name = _(u'Search Term'),
max_length = 255,
default = None
)
class SearchQuery(models.Model):
term = models.ForeignKey(
SearchTerm
)
user = models.ForeignKey(
User,
blank = True,
null = True,
)
when = models.DateTimeField(
verbose_name = _(u'Date Searched'),
)
search/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'^search/', 'search.views.search', name="search"),
)
then of course in my project urls I added the following rule:
url(r'^', include('search.urls')),
search/views.py
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render, redirect
from haystack.forms import ModelSearchForm, SearchForm
from haystack.views import SearchView
from search.models import SearchQuery, SearchTerm
import datetime
def search(request):
if 'page' not in request.GET :
try :
st, created = SearchTerm.objects.get_or_create(query=request.GET['q'])
sq = SearchQuery()
sq.term = st
sq.user = request.user
sq.when = datetime.datetime.now()
sq.save()
except :
pass
view = SearchView()
return view(request)
if 'page' not in request.GET
so that only on the first page of search results, a SearchQuery is saved
each SearchQuery is unique in the DB.
each SearchQuery for a SearchTerm is recorded with additional info such as user
(nullable) and when
the term was searched.
view = SearchView()
and return view(request)
make it so that aftera a SearchQuery is recorded, the default haystack views get called for that request.
5π
Probably the easiest solution is to subclass SearchView and use it instead in your urlpatterns.
Haystack SearchView is unfortunately written in different manner than Django Class Based Views (probably because it was written before CBV were introduced to Djangoβ¦), but it is very straightforward piece of code https://github.com/toastdriven/django-haystack/blob/master/haystack/views.py#L13
Something like this should work:
class LoggingSearchView(SearchView):
def create_response(self):
logger.info(self.query) #or log self.query as you like
return super(LoggingSearchView, self).create_response()
- [Django]-Django on Heroku, url template tag causes 'ParseResult' object not callable
- [Django]-Django 1054 β Unknown Column in field list
- [Django]-Django Gunicorn: Unable to find application
- [Django]-How to expose manytomany field as a resource with tastypie?
- [Django]-Django Forms for generic relationships. How to include them?
- [Django]-Django Allauth Ignoring Adapter
- [Django]-Django: need help with keeping apps from depending on one another
- [Django]-What's your favorite way to test the javascript in your djangoapp?