Preparing data using a model attribute that is a manager

253 views
Skip to first unread message

LaundroMat

unread,
Jun 11, 2010, 4:39:43 AM6/11/10
to django-haystack
Hi -

I'm trying to use django-taggit with haystack, and I've run into the
following problem:
From the documentation (http://docs.haystacksearch.org/dev/
searchindex_api.html#prepare-self-object) I got this:


def prepare(self, object):
self.prepared_data = super(ItemIndex, self).prepare(object)
# Add in tags (assuming there's a M2M relationship to Tag on the
model).
# Note that this would NOT get picked up by the automatic
# schema tools provided by Haystack.
self.prepared_data['tags'] = [tag.name for tag in object.tags.all()]

But, the relation in the model for tags is not an M2M relation.
Instead, the attribute tags is an instance of
taggit.TaggableManager(). This way, rebuilding the index does not
work, because the attribute "tags" is not found...

Thanks in advance for any help you might be able to give,

Mathieu

LaundroMat

unread,
Jun 11, 2010, 11:07:01 AM6/11/10
to django-haystack
Just to inform you of further progress:

I was initially using django-tagging instead of django-taggit. I
thought that taggit's approach (where "tags" is a model attribute)
would facilitate integration with haystack. I have now switched back
to django-tagging, but the question more or less remains the same.

My prepare method now looks like this:

def prepare(self, object): # See
http://docs.haystacksearch.org/dev/searchindex_api.html#prepare-self-object
self.prepared_data = super(ItemIndex, self).prepare(object)

self.prepared_data['tags'] = " ".join([tag.name for tag in
Tag.objects.get_for_object(object)])

And it seems I'm having no problems rebuilding the index anymore. Now
I'm off to experiment with filtering and such :)

Daniel Lindsley

unread,
Jun 15, 2010, 2:02:10 AM6/15/10
to django-...@googlegroups.com
LaundroMat,


Obviously, seeing more of your code would help. I've successfully
used taggit with Haystack several times. All your ought to need to do
is something like:

======
class EntrySearchIndex(indexes.SearchIndex):
text = indexes.CharField(document=True, use_template=True)
# Your fields here, then...
tags = indexes.MultiValueField()

def prepare_tags(self, obj):
return [tag.name for tag in obj.tags.all()]
====

You don't need to use the ``prepare`` override in this case,
because you already have a ``tags`` field and because you don't need
to access the prepared results of more than one field.


Daniel

> --
> You received this message because you are subscribed to the Google Groups "django-haystack" group.
> To post to this group, send email to django-...@googlegroups.com.
> To unsubscribe from this group, send email to django-haysta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-haystack?hl=en.
>
>

francofuji

unread,
Jul 6, 2010, 10:55:20 AM7/6/10
to django-haystack
And how then can i search on a MultiValueField?
> >http://docs.haystacksearch.org/dev/searchindex_api.html#prepare-self-...

Greg Brown

unread,
Jul 6, 2010, 6:03:31 PM7/6/10
to django-...@googlegroups.com
Off the top of my head, you should be able to do

SearchQuerySet().filter(tags="tag1")

in order to get all results with the tag "tag1"

Greg


Reply all
Reply to author
Forward
0 new messages