I am filtering userprofiles via their interests using Haystack. How can I use Haystack's result to query a model to return me all selected users belonging to these userprofiles?
Right now I am doing it the following way, but it is really slow, as haystack_results can be a list of many thousands of entries:
haystack_results = SearchQuerySet().raw_search('coffee AND django_ct:common.profile').values_list('pk', flat=True)
User.objects.filter(profile__id__in=haystack_results)
User and Profile have a OneToOne Relationship:
class Profile(models.Model):
user = models.OneToOneField(...)
Do you know any better way?
Thanks for your help, Matthias
You could do one of the following:
SearchIndexindexing theUsermodel using the data fromProfile(and query using.models(User))ProfileIndex(I'm assuming that's what you named it), storing theprofile's.user_id. Use that in yourvalues_list, andprofile__id__inbecomesid__in.load_allto support prefetching related data (unlikely to happen soon)Profileinherit fromUser. Note: I do not know for sure if Django does a join each time, or a lazy load once a parent field is requested.