Skip to content

Commit

Permalink
Fixed case where SearchQuerySet.more_like_this would fail when usin…
Browse files Browse the repository at this point in the history
…g deferred Models. Thanks to Alex Gaynor for the original patch.
  • Loading branch information
toastdriven committed Nov 22, 2009
1 parent 1afbdc4 commit d4246a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ Thanks to
* Frank Wiles for documentation patches.
* Chris Adams (acdha) for various patches.
* Kyle MacFarlane for various patches.
* Alex Gaynor (alex) for help with handling deferred models with More Like This.
9 changes: 8 additions & 1 deletion haystack/backends/solr_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models.loading import get_model
from django.db.models.sql.query import get_proxied_model
from django.utils.encoding import force_unicode
from haystack.backends import BaseSearchBackend, BaseSearchQuery, log_query
from haystack.exceptions import MissingDependency, MoreLikeThisError
Expand Down Expand Up @@ -184,7 +185,13 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
def more_like_this(self, model_instance, additional_query_string=None,
start_offset=0, end_offset=None,
limit_to_registered_models=True, **kwargs):
index = self.site.get_index(model_instance.__class__)
# Handle deferred models.
if hasattr(model_instance, '_deferred') and model_instance._deferred:
model_klass = get_proxied_model(model_instance._meta)
else:
model_klass = type(model_instance)

index = self.site.get_index(model_klass)
field_name = index.get_content_field()
params = {
'fl': '*,score',
Expand Down
8 changes: 8 additions & 0 deletions tests/solr_tests/tests/solr_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,14 @@ def test_more_like_this(self):
alt_mlt_with_models = self.sqs.models(MockModel).more_like_this(MockModel.objects.get(pk=1))
self.assertEqual(alt_mlt_with_models.count(), 23)
self.assertEqual([result.pk for result in alt_mlt_with_models], ['6', '14', '4', '10', '22', '5', '3', '12', '2', '23', '18', '19', '13', '7', '15', '21', '9', '20', '16', '17', '8', '11'])

if hasattr(MockModel.objects, 'defer'):
# Make sure MLT works with deferred bits.
mi = MockModel.objects.defer('foo').get(pk=1)
self.assertEqual(mi._deferred, True)
deferred = self.sqs.models(MockModel).more_like_this(mi)
self.assertEqual(alt_mlt_with_models.count(), 23)
self.assertEqual([result.pk for result in alt_mlt_with_models], ['6', '14', '4', '10', '22', '5', '3', '12', '2', '23', '18', '19', '13', '7', '15', '21', '9', '20', '16', '17', '8', '11'])


class SolrRoundTripSearchIndex(indexes.RealTimeSearchIndex):
Expand Down

0 comments on commit d4246a0

Please sign in to comment.