使用Haystack / Whoosh与索引相关的表 [英] Index related table using Haystack/Whoosh
本文介绍了使用Haystack / Whoosh与索引相关的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何索引相关表:
class Foo(models.Model):
name = models.CharField(max_length=50)
Class FooImg(models.Model):
image = models.ImageField(upload_to='img/', default = 'img/no-img.jpg',
verbose_name='Image', )
foo = models.ForeignKey(Foo, default=None, null=True, blank=True)
我想索引FooImg,以便获取与Foo相关的图像。
I want to index FooImg, so that I can get the images associated with Foo.
我有已经对Foo进行了索引,并且效果很好,它返回了预期的结果。所以在我的模板中,我有:
I have already indexed Foo, and it works perfectly fine, it returns expected result. So in my template I have:
{% for r in foo_search %}
{{ r.object.name | slice:":18" }}
{% endfor %}
以上方法有效,但我不知道如何获取关联的FooImg对象?
The above works, but I can't figure out how I can get the associated FooImg objects?
寻找方向,
推荐答案
在您的 fk
中添加 related_name
:
foo = models.ForeignKey(Foo, default=None, null=True, blank=True, related_name='images')
然后获取图像并用它们做您需要的事情,可能将它们循环:
Then get the images and do what you need with them, probably loop over them:
obj.images.all()
这篇关于使用Haystack / Whoosh与索引相关的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文