English 中文(简体)
访问与 Haystack 搜索关联的图像字段QuerySet
原标题:Accessing ImageFields associated with Haystack SearchQuerySet

我有一个图像字段模型, 我用 Django Haystack 进行索引搜索, 我喜欢在搜索结果中使用这些图像。 如果我错了, 请纠正我, 但是您不能在 Django Haystack 搜索QuerySet 中拥有图像字段 。 那么, 访问与搜索查询Set 相关的模型图像字段的最佳方法是什么? 我是否真的要绕过模型代号并将其添加到一个单独的 QuerySet 中?

最佳回答

只需使用 结果.object.my_image_field object 属性包含与搜索结果相关的完整的Django对象。

问题回答

访问对象进入数据库。

如果您不想访问数据库, 您可以这样索引 :

class MyIndex(SearchIndex, Indexable):
   ...
   photo_url = CharField()

   def prepare_photo_url(self, obj):
       return obj.image_field.path

http://django-haystack.readthedocs.org/en/latest/searchindex_api.html#prepare-foo-self-object" rel=“nofollow”>http://django-haystack.readthedocs.org/en/latest/searchindex_api.html#prepare-foo-self-object

我知道问题很老了 但我也有同样的问题 我还有信息要分享

跳到“ 强” 解答 < / 强” 。

说你有这个模型:

class School(models.Model):
  city = models.CharField(max_length=50)
  name = models.CharField(max_length=50)
  slug = models.SlugField(max_length=50, unique=True)
  cover = models.ImageField(upload_to= images/school-covers/ )

您可以看到“ 坚固” 图像字段 < / 坚固” : 封面 。

如果您想要用自动完成功能用 Haystack 索引学校模式, 您应该这样做 :

class SchoolIndex(indexes.SearchIndex, indexes.Indexable):
  name = indexes.EdgeNgramField(model_attr= name )
  city = indexes.EdgeNgramField(model_attr= city )
  slug = indexes.CharField(model_attr= slug )

  def get_model(self):
    return School

  def index_queryset(self, using=None):
    # Used when the entire index for model is updated.
    return self.get_model().objects.all()

您可以看到,我没有包括图像字段,因为图像字段对象不是 indexs 软件包的一部分。

此代码片段将使用学校模式的 < code> name 和 < code> ential 字段索引。 在您的 AJAX 视图中, 您将使用搜索查询设置处理搜索 :

def search_ecoles(request):
  name = SearchQuerySet().autocomplete(name=request.POST.get( search_text ,  ))
  city = SearchQuerySet().autocomplete(city=request.POST.get( search_text ,  ))

  schools = name | city

  return render(request,  website/ajax_search.html , { schools  : schools})

至今为止,我能够在我的模板中 访问结果,像这样:

<ul>
  {% for school in schools %}
  <li>
    <span id="name"><a href="/school/{{ school.slug }}/">{{ school.name }}</a></span>
    <span id="city">City: {{ school.city }}</span>
  </li>
  {% endfor %}
</ul>

Solution

但也许你喜欢访问封面图像字段。 最简单和可能更整洁的方法就是 使用校内物体,就像这样:

<img src="{{ school.object.cover.url }}" />




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签