Django Haystack外键关系

时间:2012-08-13 08:38:26

标签: python django django-models solr django-haystack

我有这样的模特:

class Photo(ImageModel):
    title = models.CharField(_('Title'), max_length=100, unique=True)
    title_slug = models.SlugField(_('slug'), unique=True)   
    name = models.CharField(max_length=50, verbose_name="Name", blank=True)


class GroupUpload(ImageModel):
    title = models.CharField(verbose_name="Group Name", max_length=75)
    date_added = models.DateTimeField(verbose_name="Date Added", 
            default=datetime.now, editable=False)
    caption = models.TextField(verbose_name="Caption", blank=True)        
    photos = models.ManyToManyField('Photo', related_name='+', 
          verbose_name=_('F'),null=True, blank=True, through='GroupuploadPhotos')

我的搜索索引:

class PostIndex(indexes.RealTimeSearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True, use_template=True)     
    date_added = indexes.DateTimeField(model_attr='date_added')

    def get_model(self):
        return Photo

    def index_queryset(self):
        return self.get_model().objects.filter(date_added__lte=
                                                    datetime.datetime.now())

现在我想获得与Photo有关的GroupUpload的对象。有了这个定义,我只得到GroupUpload中的Photo对象不相关的对象。我检查过doc。但还没有解决。

1 个答案:

答案 0 :(得分:3)

如果你将related_name='+'放在class GroupUpload中,会发生什么事?

<强>更新

  

为什么不设置related_name ='group_uploads'并使用   photo.group_uploads.all()(或者如果它作为haystack SearchResult -   result.object.group_uploads.all())?

问候