Thursday, July 24, 2014

display Foreign Key values in Django haystack search results

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/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAL4xV_A-1Wqah_2TGWDp-9K254bovDrBoh1%2B9xmudjJ0k240Tg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment