i have 3 form fields want modify/change value of in views - function one_labeling() before posting form in template. first field label, want change initial value in views. secondly have 2 choice fields, pos_tag , head_tags use set_head_tags , set_post_tags in views multiplechoicefields post_tags , head_tags in forms in order post value of each selected field instead of number, e.g "nn" instead of 1. @ moment value "nn" key 1 pos_tags in forms wonder if set , modify initial variable in views field pos_tags , head_tags 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 , data2: l = data1[0] ll = data2[0] pos_tags = dict(form.fields['pos_tags'].choices) head_tags = dict(form.fields['head_tags'].choices) = 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 multiple choice fields, can try doing following :-
pos_tags = forms.multiplechoicefield(choices=pos_nodes, initial=[pos_node[0] pos_node in pos_nodes])
Comments
Post a Comment