Home > Software design >  Django Haystack update index only for 1 model
Django Haystack update index only for 1 model

Time:12-16

I am currently trying out Django haystack to update data from PostgreSQL to a solr collection.

So, I have defined 2 models in search_indexes.py. So, when I run the command python manage.py update_index it indexes the data from both the models defined in search_indexes.py to my solr collection.

HOW DO I PERFORM update_index OPERATION ONLY FOR A SPECIFIC MODEL THAT I NEED?

Currently, when I run the command, the following 2 models ran.

Indexing 2 model1
Indexing 12 model2

search_indexes.py

from haystack import indexes
from .models import table1, table2

class model1(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(
        document=True,
        use_template=True,
        template_name="search/indexes/tenants/table1_text.txt"
    )
    ats_id = indexes.CharField(model_attr='ats_id')
    ats_name = indexes.CharField(model_attr='ats_name')
    added_by = indexes.CharField(model_attr='added_by')
    added_on = indexes.DateTimeField(model_attr='added_on')

    def get_model(self):
        return table1

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

class model2(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(
        document=True,
        use_template=True,
        template_name="search/indexes/tenants/table2_text.txt"
    template_id = indexes.CharField(model_attr='template_id')
    template_name = indexes.CharField(model_attr='template_name')
    aspect = indexes.CharField(model_attr='aspect')
    version = indexes.CharField(model_attr='version')
    added_by = indexes.CharField(model_attr='added_by')
    added_on = indexes.DateTimeField(model_attr='added_on')
    ats_id = indexes.CharField(model_attr='ats_id')

    def get_model(self):
        return table2

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

Please suggest a workaround.

CodePudding user response:

# Update just a single model (in a complex app).
./manage.py update_index models.model1

docs to update_index

  • Related