display Foreign Key values in Django haystack search results

19 views
Skip to first unread message

Kannan

unread,
Jul 24, 2014, 11:34:20 AM7/24/14
to django...@googlegroups.com

Hello Everyone.. 

I have posted this question in Stackoverflow. I didn't not get answers so far.. posting here to hope some help from you guys.. 

http://stackoverflow.com/questions/24936919/display-foreign-key-values-in-django-haystack-search-results


I am new to django and haystack. Can anyone please help me with this code. I have the below Models in models.py file

class UserProfile(models.Model):
    street_address = models.CharField(max_length=100, blank=False, null=False)
    city = models.CharField(max_length=100, blank=False, null=False)
    zip_code = models.PositiveIntegerField(blank=False, null=True)

class UserProfessional(models.Model):
    userprofile = models.ForeignKey(UserProfile, unique=True, related_name = 'user_profiles')
    company = models.CharField(max_length=200, blank=False, null=False)
    professional_type = models.CharField(max_length=50, choices=PROFESSIONAL_TYPE)
    bio_exp = models.CharField(max_length=300)

The search criteria would be zip_code, city and state. So we can search by zip code but the results should display the user_profile, company, and bio_exp.

I have been trying the following code.. but it is not working as expected.

class UserProfileIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True,use_template=True)
    name = indexes.CharField()

    def get_model(self):
        return UserProfile

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.all().select_related('user_profiles')

    def prepare_name(self, obj):
        return obj.user_profiles.bio_exp

--
With regards,

Kannan. R. P,
Blog @: http://kannan4k.wordpress.com/
Reply all
Reply to author
Forward
0 new messages