在搜索haystack中显示子模型数据

时间:2013-06-06 05:13:22

标签: django django-haystack

我使用haystack-solr进行搜索,我想做的是当一个人搜索教育时它显示用户的所有数据是外键 我的模特:

class user_info(models.Model):

    gender= (

        ('Male',_('Male')),
        ('Female',_('Female')),

    )
    user_id = models.AutoField(primary_key=True)
    first_name = models.CharField(verbose_name=_('First Name'),max_length=100, null=True, blank=True)
    last_name = models.CharField(verbose_name=_('Last Name'),max_length=100, null=True, blank=True)
    industry_id = models.ForeignKey(industry,verbose_name=_('Industry ID'),to_field = 'industry_id',  null=True, blank=True)
    functional_area_id = models.ForeignKey(functional_area,verbose_name=_('Functional Area'),to_field = 'functional_area_id',  null=True, blank=True)
    city = models.CharField(verbose_name=_('City'),max_length=100, null=True, blank=True)
    country= models.ForeignKey(Country,verbose_name=_('Country'),to_field = 'country_name',  null=True)
    phone_number1 = models.CharField(verbose_name=_('Phone Number 1'),max_length=100, null=True, blank=True)
    phone_number2 = models.CharField(verbose_name=_('Phone Number 2'),max_length=100, null=True, blank=True)
    phone_number3 = models.CharField(verbose_name=_('Phone Number 3'),max_length=100, null=True, blank=True)
    cnic = models.CharField(verbose_name=_('Citizen National Identity Card'),max_length=100, null=True, blank=True)
    dob =    models.DateField(verbose_name=_('Date of Birth'))
    gender =    models.CharField(verbose_name=_('Gender'),max_length=255, choices=gender,default='Male')


    def __unicode__(self):
        #return self.user
        return unicode(self.first_name)

admin.site.register(user_info)

class resume(models.Model):

    user_id = models.ForeignKey(user_info,verbose_name=_('User ID'),to_field = 'user_id',  null=True, blank=True)
    resume_id = models.AutoField(primary_key=True)

    def __unicode__(self):
        #return self.user
        return unicode(self.resume_id) 

admin.site.register(resume)

class education(models.Model):

    education_id = models.AutoField(primary_key=True)
    resume_id = models.ForeignKey(resume,verbose_name=_('Resume ID'),to_field = 'resume_id',  null=True, blank=True)
    title = models.CharField(verbose_name=_('Degree Title'),max_length=100, null=True, blank=True)
    educational_level_id = models.IntegerField (verbose_name=_('Educational Level ID'),max_length=255,null=True)
    institute = models.CharField(verbose_name=_('Institute'),max_length=100, null=True, blank=True)
    start_date =    models.DateField(verbose_name=_('Start date'),null=True)
    end_date =      models.DateField(verbose_name=_('End date'),null=True)
    cgpa = models.DecimalField(verbose_name=_('CGPA'),max_digits=10,decimal_places=2,default="0.00")
    percent = models.DecimalField(verbose_name=_('Percent'),max_digits=10,decimal_places=2,default="0.00")
    majors = models.CharField(verbose_name=_('Majors'),max_length=2000, null=True, blank=True)

    def __unicode__(self):
        #return self.user
        return unicode(self.title)

admin.site.register(education)

我的search_indexes.py

class resume(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    user_id = indexes.MultiValueField()

    def prepare_resumes(self, obj):
        # Use the relation and hang the id's off it.
        return [user_info.user_id for user_id in obj.user_info.all()] 

    def get_model(self):
        return resume


class UserIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    city = indexes.CharField(model_attr='city')
    dob = indexes.CharField(model_attr='dob')
    first_name = indexes.CharField(model_attr='first_name')
    last_name = indexes.CharField(model_attr='last_name')
    phone_number1 = indexes.CharField(model_attr='phone_number1')
    phone_number2 = indexes.CharField(model_attr='phone_number2')
    phone_number3 = indexes.CharField(model_attr='phone_number3')
    cnic = indexes.CharField(model_attr='cnic')
    gender = indexes.CharField(model_attr='gender')
    resumes = indexes.MultiValueField()

    def get_model(self):
        return user_info

class EducationIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')
    educational_level_id = indexes.CharField(model_attr='educational_level_id')
    institute = indexes.CharField(model_attr='institute')
    cgpa = indexes.CharField(model_attr='cgpa')
    percent = indexes.CharField(model_attr='percent')
    majors = indexes.CharField(model_attr='majors')
    resume_id = indexes.MultiValueField()

    def prepare_resumes(self, obj):
        # Use the relation and hang the id's off it.
        return [resume.resume_id for resume_id in obj.resume.all()] 

    def get_model(self):
        return education

现在这是一对多的关系,如果我搜索用户,它应该向我显示用户以及教育信息,当我搜索教育时,它应该向我显示用户信息。 怎么办呢?

0 个答案:

没有答案