Update reference models in Haystack problem solved


When we put reference model in part of index and get updated, it will NOT changed in main model. Here is some example:

models.py

1
2
3
4
5
class UserProfile(CommonInfo):
    city = models.ForeignKey(City)

    def __unicode__(self):
        return unicode(self.code)

With search indexes :

userprofile.txt

1
{{ object.city.name }}

When “CITY” models is updated, let say “CHICAGO” into “UTAH”. We search on UserProfile, the city name will be still “CHICAGO”. Whooppss!

The solution, whenever City updated, we need to update UserProfile as well. Create signal to save UserProfile whenever City updated with post_save()


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.