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:
SearchIndex
indexing theUser
model 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__in
becomesid__in
.load_all
to support prefetching related data (unlikely to happen soon)Profile
inherit 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.