Search code examples
pythondjangodjango-formsdjango-haystack

Django Haystack - initial value for fields in overriden SearchForm not being displayed


On a site I've been running for years, I just upgraded both Django and Haystack from 1.11.5 and 2.6.1, respectively, to 2.0.5 and 2.8.1, respectively. Everything with the upgrade seems to have gone smoothly except that the initial values I'm providing in the overriden SearchForm are not being used anymore. I can't figure out why. I posted an issue on Haystack's issue tracker here, but I haven't received a response, so I'm hopeful someone here might have some insight.

Here's the form:

from haystack.forms import SearchForm

class SearchForm(SearchForm):                                            
    start_date = forms.DateField(initial=Post.get_first_post_timestamp(),
                                 input_formats=['%Y-%m-%d'])
    end_date = forms.DateField(initial=datetime.date.today,
                               input_formats=['%Y-%m-%d'])
    thread_type = forms.ChoiceField(choices=[('All Types', 'All Types')] + Thread.get_sorted_unique_types_choices())

Here's the view:

import haystack.generic_views

from apps.listserv.forms import SearchForm

class SearchView(haystack.generic_views.SearchView):
    template_name = 'listserv/search.html'
    form_class = SearchForm
    results_per_page = 10

Here's the template:

...
[{{ form.start_date.value }}]

<form action="." method="get">
    <p style="text-align: center; font-weight: bold;">Search: {{ form.q }}</p>
    <p style="text-align: center; font-weight: bold;">Dates: {{ form.start_date }} to {{ form.end_date }}</p>
    <p style="text-align: center; font-weight: bold;">Thread Type: {{ form.thread_type }}</p>
    <p style="text-align: center;"><input type="submit" value="Search"></p>
</form>
...

Here's a screenshot of the resulting render:

render

As you can see, neither date is being displayed.

Note that prior to the upgrades, this did work. I'm wondering if something has changed between then and now in how initial parameters are handled with Haystack. I've checked the Django and Haystack changelogs pretty extensively, but nothing jumps out at me.

Does anyone have any guidance on this? Thanks!

[EDIT - 2017-05-09]
I just tried downgrading only Haystack back to 2.6.1, and this does work. So something must've changed with how Haystack's SearchForm handles initial parameters since then.

I did a little more debugging by trying each release listed here. 2.7.dev0 works, but 2.7.0 does not. So it looks like the bug I'm experiencing was introduced between those two versions. Note that I'm capturing all of this information on the issue I mentioned above (here).


Solution

  • I just figured out what's going on. After some debugging, I found the commit that introduced this bug (see https://github.com/django-haystack/django-haystack/issues/1605#issuecomment-387938032 for specifics).

    I've pointed the exact bug out to the Haystack guys, so hopefully they'll respond soon with a fix.