set field's initial value within haystack's FacetedSearchForm

雨燕双飞 提交于 2019-12-07 14:18:33

As django.forms.Form set initial data only to an unbound form setting inital is not an option here. We should rather pass the inital values as data to the build_form method in haystack.views.FacetedSearchView as this is where form is initialized.

Example:

MySearchView(FacetedSearchView):

    def build_form(self, form_kwargs=None):
        """ Overrides the build_form method to send initial data
        """

        form = super(MySearchForm, self).build_form(form_kwargs)
        form_data = form.data.copy() # Need to copy this as QueryDict is immutable
        # Set initial data hete
        form_data['field_name'] = 'fields_inital_value'
        form.data = form_data

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