Index related table using Haystack/Whoosh

吃可爱长大的小学妹 提交于 2020-01-06 03:06:45

问题


How can I index a related table:

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)

I want to index FooImg, so that I can get the images associated with 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 %}

The above works, but I can't figure out how I can get the associated FooImg objects?

Looking for direction,


回答1:


Add related_name to your fk:

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()


来源:https://stackoverflow.com/questions/45004561/index-related-table-using-haystack-whoosh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!