django - set field's initial value within haystack's FacetedSearchForm -
i using django-haystack
solr
backend in 1 of projects. have searchform inherits facetedsearchform
. now, trying add intital value of fields within form.
from django.forms import forms haystack.forms import facetedsearchform mysearchform(facetedsearchform): """ basic search form """ field_one = forms.charfield(required=false, initial='0') field_two = forms.charfield(required=false, initial='some_value')
previously when using django.forms.form
used work absolutely fine. have been digging around django code , haystack code , fond facetedsearchform
extends haystack.forms.searchform
extends django.forms.forms
. can not see reason why should not work.
references:
i tried overriding __init__
method , thought set initial values there:
def __init__(self, *args. **kwargs): data = kwargs.pop('data', none) if data: values = { 'field_one': (data.get('field_one', none), '0'), 'field_two': (data.get('field_two', none), 'some_value') } value in values: if not values[value][0]: self.fields[value].initial = values[value][3] super(mysearchform, self).__init__(*args, **kwargs)
but in facetedsearchform
there no fields
attribute, though provide access base_fields
object, instead tried doing:
self.base_fields[value].initial = values[value][4]
which not work either.
strange part came when realized not raise errors or exceptions, tried printing out initial values debug , show inital values set.
but when form rendered in template fields not have initial values. values fields none both fields in template.
i tried checking value using:
{{ form.field_one.value }} {{ form.field_two.value }}
and output is:
none
none
have ran issue before ? how can have inital values show in template when form rendered ?
as django.forms.form
set initial data only unbound form setting inital not option here. should rather pass inital values data build_form
method in haystack.views.facetedsearchview
form initialized.
example:
mysearchview(facetedsearchview): def build_form(self, form_kwargs=none): """ overrides build_form method send initial data """ form = super(mysearchform, self).build_form(form_kwargs) form_data = form.data.copy() # need copy querydict immutable # set initial data hete form_data['field_name'] = 'fields_inital_value' form.data = form_data return form
Comments
Post a Comment