Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django Haystack update index only for 1 model

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

docs to update_index

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading