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.
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)
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])