问题描述
我正在使用在我的一个项目中具有 Solr
后端。我有一个SearchForm,它继承自 FacetedSearchForm
。现在,我想做的是向表单中的某些字段添加初始值。
I am using django-haystack
with Solr
backend in one of my projects. I have a SearchForm which inherits from FacetedSearchForm
. Now, what I am trying to do is to add an intital value to some of the fields within the Form.
from django.forms import forms
from haystack.forms import FacetedSearchForm
MySearchForm(FacetedSearchForm):
""" My basic search form
"""
field_one = forms.CharField(required=False, initial='0')
field_two = forms.CharField(required=False, initial='some_value')
以前,当我使用 django.forms.Form
时,此方法工作得非常好。我一直在研究Django代码和干草堆代码,并喜欢 FacetedSearchForm
扩展了 haystack.forms.SearchForm
并扩展了 django.forms.Forms
。因此,我看不出为什么它不起作用。
previously when i was using django.forms.Form
this used to work absolutely fine. I have been digging around django code and haystack code and fond that FacetedSearchForm
extends haystack.forms.SearchForm
which extends django.forms.Forms
. So I can not see a reason why this should not work.
参考文献:
- haystack.forms.FacetedSearchForm
- django.forms.Form
我还尝试覆盖 __ init __
方法,并认为我会设置
I also tried overriding the __init__
method and thought I would set the 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')
}
for value in values:
if not values[value][0]:
self.fields[value].initial = values[value][3]
super(MySearchForm, self).__init__(*args, **kwargs)
但在 FacetedSearchForm
没有 fields
属性,尽管它们提供对 base_fields
的访问对象,所以我改为尝试做:
but in a FacetedSearchForm
there is no fields
attribute, though they provide access to base_fields
object, so I instead tried doing:
self.base_fields[value].initial = values[value][4]
这也不起作用。
奇怪的部分是当我意识到它不会引发任何错误或例外,所以我尝试打印出初始值进行调试,并很好地表明已设置了初始值。
Strange part came when I realized that It does not raise any errors or exceptions, so I tried printing out the initial values to debug and well it does show that inital values are set.
但是,当表单在模板中呈现时,字段没有那些初始值价值观。
But when the form is rendered in the template the fields does not have those initial values. The values for the fields are None for both fields in the template.
我尝试使用以下方法检查该值:
I tried checking the value using:
{{ form.field_one.value }}
{{ form.field_two.value }}
输出为:
无
之前有人遇到过这个问题吗?呈现表单时,如何在模板中显示初始值?
Have anyone ran into this issue before ? How can I have inital values show up in the template when the form is rendered ?
推荐答案
为 django .forms.Form
只能将初始数据仅设置为未绑定的表单,因此设置inital此处不是一个选项。我们应该将初始值作为数据传递给 haystack.views.FacetedSearchView
中的 build_form
方法
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.
示例:
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
这篇关于在干草堆的FacetedSearchForm中设置字段的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!