views:

36

answers:

1

I'm trying to get haystack (with xapian-haystack) to search my model here by name and description.

I have a subclass of item ('device') which has a manufacturer field.

So I have a model 'item':

class Item(models.Model):
    name = models.CharField(max_length=255, unique=True)
    description = models.TextField(null=True, blank=True)
    compatible_with = models.ManyToManyField('self', null=True, blank=True)
    often_with = models.ManyToManyField('self', null=True, blank=True)
    created_by = models.ForeignKey(User, null=True, blank=True, related_name='created_by')
    verified = models.BooleanField(default=False)
    verified_by = models.ForeignKey(User, null=True, blank=True, related_name='verified_by')
    date_created = models.DateField(auto_now_add=True)
    slug = models.SlugField(max_length=300, null=True, blank=True)

My index class looks like this:

class ItemIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    name = CharField(model_attr='name')
    description = CharField(model_attr='description')

site.register(Item, ItemIndex)

I have templates/search/indexes/catalog/item_text.txt:

{{ object.name }}
{{ object.description }}

What do I add to item_text.txt to add the manufacturer if and only if the object is an instance of 'device'?

+1  A: 
{% if device.manufacturer %}
{{ device.manufacturer }}
{% endif %}

... the haystack tutorial is a bit confusing on this subject (you don't actually have to use a text-file template, for one) but the basic idea is that haystack's engine goes to town on whatever text data is in this template.

actually, it goes to town on whatever is in the response you send it, but if you've got the template set up you can use whatever django template logic you want in there.

(note that the "if" template tag was a bit of a dog's breakfast prior to django 1.2; if you're stuck on an earlier version you may have to tweak this but the principle is the same.)

fish2000
Thanks :) this did the trick.
Sri Raghavan
glad to hear it.
fish2000