2

I have these two models:

class Shop(CustomBaseModel):
    username = models.CharField(max_length=200)
    bio = models.CharField(max_length=200)


class Item(CustomBaseModel):
    shop = models.ForeignKey(Shop)
    tags = models.TextField()

And I have this index in search_indexes.py:

class ItemIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    tags = indexes.CharField(model_attr='tags')

And this item_text.txt file:

{{ object.tags}}

This is my view:

def search(request):
    form = ItemSearchForm(request.GET)
    results = form.search()
    return render(request, 'search/search.html', {
        'items': results
    })

This is my form:

class ItemSearchForm(SearchForm):
    def search(self):
        sqs = super(ItemSearchForm, self).search().models(Item, Shop)
        return sqs

This is the template:

 {% if items|length > 0 %}
        <h1>Items</h1>
        {% for item in items %}
            <p>
                {{ item.tags }}
            </p>
        {% endfor %}
 {% endif %}

This is searching from tags and working fine. But how can I display all items that belong to a shop if someone includes a username or bio of that shop?

Rodrigue
  • 169
  • 13
  • 1
    http://stackoverflow.com/questions/6804809/haystack-how-you-display-data-from-multiple-models-with-foreignkeys – madzohan Nov 04 '14 at 08:20
  • And what are you doing in your template with the variable `items` ? Check my edit in my answer – AlvaroAV Nov 04 '14 at 09:26
  • I included the template code. @Liarez – Rodrigue Nov 04 '14 at 09:29
  • I suggest you to check my edit2 and change your **items_text.txt** file and add `{{item.shop.username}}` and `{{item.shop.bio}}`, so if you look for shop username it will return all items belonging to it – AlvaroAV Nov 04 '14 at 09:44

1 Answers1

2

When using Haystack and you got the result query set you have a list of objects, the fields of this objects depends on what you've wrote in:

templates/search/indexes/project_name/modelName_text.txt

But there is allways a field that can help you here, the field object

If I understand your question you want to know:

  • How to show all items of a shop when an user look for a shop using its username or bio

I'll show you how to do it in command line, you can apply this code to your view/template:

from haystack.query import SearchQuerySet

# First we make a search to get some shops 
# assuming you defined username field in search_indexes.py for Shop object
shop_list = SearchQuerySet().filter(username='something')  
# or
shop_list = SearchQuerySet().filter(content='shop_username')  
# Now (let's supose there is at least 1 shop) you can get the items like:    
shop_list[0].object  # This returns the shop object, so you can do
shop_list[0].object.item_set.all()  # This returns all items belonging to this shop

In your template you could try:

{{ object.object.item_set.all }}  # This return a list of items belonging to shop

You could generate a list like this

<ul>
{% for item in object.object.item_set.all %}
     <li>{{item}}</li>
{% endfor %}
</ul>

This can be confused because you called object to your variable, but you need to remember that a SearchQuerySet instance allways have this object field to access the original Django object

I recommend you to check Haystack - SearchQuerySet API


Edit

In your template your results are in the variable items so you can do something like:

# Assumming all the items are shops
{% for shop in items %}  
   {{shop.object.item_set}}  # If the item is a SHop, this return all its items
{% endfor %}

To separate the results, because I supose not all the items are shops, you can separate the results and send the shops in another variable to the template like:

def search(request):
    form = ItemSearchForm(request.GET)
    results = form.search()
    return render(request, 'search/search.html', {
        'items': results
        'shops': results.model(Shop)
    })

So in your template you can do:

{% for shop in shops %}  
   {{shop.object.item_set}} 
{% endfor %}

Edit2

Okay now it looks you have items in your template... and you want all the items of a shop ?

  • Wich shop ? Same shop as the items in template ?

You said:

But how can I display all items that belong to a shop if someone includes a username or bio of that shop?

So you're using haystack to search for items, but want the results to be shops ?

You could add into your item_text.txt file:

{{ object.tags}}
# If you add this and you look for shop username 
# or bio it will return all items of the shop
{{ object.shop.username}} 
{{ object.shop.bio}} 
# Adding this 2 fields items are indexed with the related shop username and bio

or you can start indexing Shop model too.

AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
  • You got my question correct. In command-line it is working fine. But I was not able to do this in my code :( so frustrated. I tried this in my template: {{ object.object.item_set.all }}. Did not help – Rodrigue Nov 04 '14 at 09:12
  • Can you show how you send the results to template ? some code of your view and template ? Because if it works on shell, it should work on template too (I use this in my projects). What do you have in {{object}} ? – AlvaroAV Nov 04 '14 at 09:14
  • items is not of Shop model. They belong to Item model. – Rodrigue Nov 04 '14 at 09:33
  • So ? You need to do item.shop.item_set... You have all the logic you need here... I cannot write and rewrite the exact code for you... Do you know the foreign key relations between object ? I missunderstood that you were looking for shops... – AlvaroAV Nov 04 '14 at 09:46
  • Sorry for my english. I want only items. When people search for items, I want to show thos items. But when they search shops I want to show items of those shops – Rodrigue Nov 04 '14 at 09:49
  • @Rodrigue enter here and we can talk, I can explain this better: [chat](http://chat.stackoverflow.com/rooms/64225/django-haystack-issue) – AlvaroAV Nov 04 '14 at 09:51
  • Thank you very much for your help) Now I kinda figured it out. Not sure how though :) – Rodrigue Nov 04 '14 at 09:53
  • I can explain a few things to you to help understanding if you like, if you're interested you can enter the chat http://chat.stackoverflow.com/rooms/64225/django-haystack-issue – AlvaroAV Nov 04 '14 at 09:53
  • It is working now. I can't enter that chat, not enough reputation) Thanx) – Rodrigue Nov 04 '14 at 09:54
  • I would love it if you could explain me things, can we chat somewhere else? jrahmonov2@gmail.com is my email, https://www.facebook.com/jahongir.rahmonov.3 - my facebook account – Rodrigue Nov 04 '14 at 09:58