Django: how to get value from CharField and ModelChoiceField

I have a GroupAdminForm class that is used to extend a group admin page in Django. There are two fields, selected_to_change and print_name. What I plan to do is select the column in "selected_to_change" and enter the char name in "print_name" to make a query like:

UPDATE "annotation" SET print_name= "value of print_name" WHERE id = "value of selected_to_change"; 

Here is the GroupAdminForm:

 class GroupAdminForm(forms.ModelForm): users = forms.ModelMultipleChoiceField(queryset=User.objects.all(), widget=FilteredSelectMultiple('Users', False), required=False) select_to_change = forms.ModelChoiceField(queryset=Annotation.objects.all(), required=False) print_name = forms.CharField(required=False) class Meta: model = Group def __init__(self, *args, **kwargs): instance = kwargs.get('instance', None) if instance is not None: initial = kwargs.get('initial', {}) initial['users'] = instance.user_set.all() initial['locations'] = instance.c_locations.all() kwargs['initial'] = initial super(GroupAdminForm, self).__init__(*args, **kwargs) def save(self, commit=True): group = super(GroupAdminForm, self).save(commit=commit) if commit: group.user_set = self.cleaned_data['users'] else: old_save_m2m = self.save_m2m def new_save_m2m(): old_save_m2m() group.user_set = self.cleaned_data['users'] self.save_m2m = new_save_m2m return group 

How can I get values ​​from CharField and ModelChoiceField to make such requests in the save () method? Thanks.

+8
python django django-models django-admin django-forms
source share
1 answer

You are already using data from a saved form in self.cleaned_data.

 print self.cleaned_data['select_to_change'] print self.cleaned_data['print_name'] 
+1
source share

All Articles