Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:15:57+00:00 2026-06-06T19:15:57+00:00

I was reading the docs and some questions around here and couldn’t understand how

  • 0

I was reading the docs and some questions around here and couldn’t understand how can I return a query that has fields indexed in two different classes. Let me put the code below:

Here I have the classes:

class T031003Index(RealTimeSearchIndex):
text = CharField(document=True, use_template=True)
C003INST = IntegerField(model_attr='C003INST')
C003CHCD = CharField(model_attr='C003CHCD')
C003MTR = CharField(model_attr='C003MTR')
C003RZSC = CharField(model_attr='C003RZSC')

def index_queryset(self):
    return T031003.objects.all()

def prepare(self, obj):
    self.prepared_data = super(T031003Index, self).prepare(obj)
    self.prepared_data['text'] = obj.C003CHCD
    return self.prepared_data

site.register(T031003, T031003Index)

And the second one:

class T031002Index(RealTimeSearchIndex):
text = CharField(document=True, use_template=True)
C002USER = CharField(model_attr='C002USER')

def index_queryset(self):
    return T031002.objects.all()

def prepare(self, obj):
    self.prepared_data = super(T031002Index, self).prepare(obj)
    self.prepared_data['text'] = obj.C002USER
    return self.prepared_data
site.register(T031002, T031002Index)

And I have two template indexes for each of them:

T031003_text:

{{ object.C003INST }}
{{ object.C003CHCD }}
{{ object.C003MTR }}
{{ object.C003RZSC }}

T031002_text:

{{ object.C002USER }}
{{ object.C002INST }}

My template code:

{% if page.object_list %}    
{% for object in page.object_list %}
    <br>
    <li><font class="font">
      {{ object.C003RZSC }}, {{ object.C003INST }}, {{ object.C003CHCD }}, {{ object.C003MTR }}, {{ object.C002USER }}
    </li>
{% endfor %}

My view:

def search(req):
return SearchView(template='search.html')(req)

If I type in the search box a value from a field, let’s say, that belongs to class T031002Index (like user = “vane”), it gives me the result:

“None, None, None, None, vane”

And, if I type a value from a field in class T031003Index, it gives me the result:

“pencil, 1, school material, general, None”

I have between these two classes in models.py a Foreign Key field, which is C002INST.

Could you guys give me an explanation? It seems easy, but I can’t figure it out by myself.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:15:59+00:00Added an answer on June 6, 2026 at 7:15 pm

    So, after a little help from close friends and a lot of research, I could find the solution.

    1)I made an upgrade from Haystack 1.2.6 to Haystack 2.0.0

    2)In my view, I changed the code to use AutoQuery and SearchQuerySet. It let’s you use more that one word to search content and let’s you filter results.

    Here’s the view:

    def search(request):
    sqs = SearchQuerySet().filter(content=AutoQuery(request.GET['q']))
    return  render_to_response('search.html', {'sqs': sqs,})
    

    4) Now, in “search_indexes.py”, I changed the code to follow 2.0.0 version of Haystack and took off the “model_attr” from the foreign key (T031002Index, C002INST field).

    Here’s the code:

    class T031003Index(RealTimeSearchIndex,Indexable):
       text = CharField(document=True, use_template=True)
       C003INST = IntegerField(model_attr='C003INST')
       C003CHCD = CharField(model_attr='C003CHCD')
       C003MTR = CharField(model_attr='C003MTR')
       C003RZSC = CharField(model_attr='C003RZSC')
    
    def index_queryset(self):
        return T031003.objects.all()
    
    def get_model(self):
        return T031003
    
    
    class T031002Index(RealTimeSearchIndex,Indexable):
        text = CharField(document=True, use_template=True)
        C002INST = CharField()
        C002USER = CharField(model_attr='C002USER')
    
    def index_queryset(self):
        return T031002.objects.all()
    
    def get_model(self):
        return T031002
    

    But even with all these changes, Haystack would iterate over the two indexes and would return null results. This is wrong and also would lead the user to confusion. So, I changed the template to show me the fields only when they have results which are not null:

    {% if page.object_list %}    
    {% for result in page.object_list %}
        {% if result.object.C003MTR%}
        <li><b>Matrícula:</b> {{ result.object.C003MTR }}</li>
        <li><b>CPF/CNPJ:</b> {{ result.object.C003CHCD }}</li>
        {% endif %}
        {% if result.object.C002INST %}
        <li><b>ID Instituição:</b> {{ result.object.C002INST }}</li>
        <li><b>Usuário:</b> {{ result.object.C002USER }}</li>
        {% endif %}
    {%endfor%}
    

    The “if” will do the trick to test if the field has null results.

    Hope it helps other developers using Haystack with Django.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some questions that I couldn't figure out from reading the docs and some other
while reading docs of backbone i can't understand how to add model to collection
I'm on 5.3.1 and after reading the docs, I see that fileinfo is included
From reading the Apple Docs on Core Data, I've learned that you should not
After reading this: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it I came to the conclusion that it is not valid
I've been reading the official docs about sending emails from Plone using some templates,
So I've spent some time checking out CocoaDev, reading the Cocoa docs on NSMenuItems,
I'm not familiar with Filnet P8. My assumptions from reading some online docs is
Reading the docs, I'd expect $(#wrap2).remove(.error) to remove all .error elements from #wrap2 .
I have been reading the docs and playing with different EventQuery parameters for days

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.