So I recently just added user search to to my site. However, when I look up by location or discipline, nothing shows up. User Profile has a link to user. For example, I have a user profile object with Los Angeles and a discipline of Journalism. But if I search, Los Angeles, nothing pops up. Even if I check the box that says "Search in User Profiles".
views.py
def search(request):
context = RequestContext(request)
render_to_response('search.html', context )
models.py
class UserProfile(models.Model):
#this line is required. Links MyUser to a User Model
user = models.OneToOneField(User, related_name ="profile")
#Additional Attributes we wish to include
date_of_birth = models.FloatField(blank=False)
phone = models.CharField(max_length=10, blank = True)
city = models.CharField(max_length=40, blank = True)
state = models.CharField(max_length=2, blank = True)
zipCode = models.CharField(max_length=5, blank = True)
admin = models.BooleanField(default=False, blank = True)
mentor = models.BooleanField(default=False, blank = True)
mentee = models.BooleanField(default=False, blank = True)
# profilepicture = models.ImageField()
#is_staff = True
tagline = models.CharField(max_length=70, blank = True, default="Let's do this!")
interests = models.ManyToManyField(Interest, related_name="interest", symmetrical = False)
primaryDiscipline = models.ForeignKey(Discipline, default=False, blank = True)
addtlDisciplines = models.ManyToManyField(Discipline, related_name="disciplines", symmetrical=False)
Haystack Specific files
search_indexes.py
import datetime
from haystack import indexes
from myapp.models import UserProfile
class UserIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
primaryDiscipline = indexes.CharField(model_attr='primaryDiscipline')
location = indexes.CharField(model_attr='city')
def get_model(self):
return UserProfile
def index_queryset(self, using=None):
return self.get_model().objects.filter(location=location)
search.html
{% extends 'base.html' %}
{% block content %}
<h2>Search</h2>
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td> </td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>
{% if query %}
<h3>Results</h3>
{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}<a href="?q={{ query }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
{% endblock %}
userprofile_text.txt
{{ object.title }}
{{ object.user.get_full_name }}
{{ object.body }}
Your problem has to do with your index_queryset. Change
to
and then rebuild your index.
index_queryset
is your default QuerySet, this is what your search will filter to give you your results. What you had before was making it so your default QuerySet filtered by what was essentiallylocation=Null
, leaving you with 0 items in your QuerySet.Source at the django-haystack docs
If you still have troubles after this please feel free to post a comment.