I have three form fields that I want to modify/change value of in my views - function one_labeling() before posting the form in the template. The first field is label, where I want to change the initial value in views. Secondly I have two choice fields, pos_tag and head_tags where I use set_head_tags and set_post_tags in views from MultiplechoiceFields post_tags and head_tags in forms in order to post the value of each selected field instead of the number, e.g "NN" instead of 1. At the moment I get the value "NN" for key 1 from pos_tags in forms but I wonder if I could set and modify an initial variable in views for field pos_tags and head_tags as with the field label.
我有三个表单字段,我想修改/更改我的视图中的值 - 函数one_labeling()之前在模板中发布表单。第一个字段是label,我想在其中更改视图中的初始值。其次,我有两个选择字段,pos_tag和head_tags,我在MultiplechoiceFields post_tags和head_tags的视图中使用set_head_tags和set_post_tags,以便发布每个所选字段的值而不是数字,例如“NN”而不是1。我从表格中的pos_tags得到键1的值“NN”,但我想知道我是否可以在字段pos_tags和head_tags的视图中设置和修改初始变量,就像字段标签一样。
in forms.py
class LabelingForm(forms.ModelForm):
label = forms.CharField(widget=forms.HiddenInput(), initial="trytrytry22",required=False)
POS_nodes = (('1','NN'),
('2','POSS'),
....
)
pos_tags = forms.MultipleChoiceField(choices=POS_nodes, required=False)
Head_node_choices = (('1','NP'),
('2','VP'),
...
)
head_tags = forms.MultipleChoiceField(choices=Head_node_choices,required=False)
class Meta:
model = OneLabeling
fields = ('label', 'sentence' )
def set_head_tags(self, head_tags):
data = self.data.copy()
data['head_tags'] = head_tags
self.data = data
def set_pos_tags(self, pos_tags):
data = self.data.copy()
data['pos_tags'] = pos_tags
self.data = data
in views.py
def one_labeling(request, postID):
form = LabelingForm(request.POST)
data1 = form.cleaned_data['pos_tags']
data2 = form.cleaned_data['head_tags']
if form.is_valid():
if data1 and data2:
l = data1[0]
ll = data2[0]
pos_tags = dict(form.fields['pos_tags'].choices)
head_tags = dict(form.fields['head_tags'].choices)
i = pos_tags.get(l)
j = head_tags.get(ll)
form.set_head_tags(j)
form.set_pos_tags(i)
post_one_labeling(request, one_labeling)
1 个解决方案
#1
For giving initial values for multiple choice fields, you can try doing the following :-
要为多选字段提供初始值,您可以尝试执行以下操作: -
pos_tags = forms.MultipleChoiceField(choices=POS_nodes,
initial=[POS_node[0] for POS_node in POS_nodes])
#1
For giving initial values for multiple choice fields, you can try doing the following :-
要为多选字段提供初始值,您可以尝试执行以下操作: -
pos_tags = forms.MultipleChoiceField(choices=POS_nodes,
initial=[POS_node[0] for POS_node in POS_nodes])