Django将表单数据复制到另一个表单

时间:2019-01-04 04:27:52

标签: python django django-forms

我有两个简单的Django表单PDEM和GUARANTOR。我正在尝试弄清楚如果Patient_Relationship = Self,那么如何将数据从PDEM表单复制到GUARANTOR表单。

class PDEMForm(forms.Form):

    first_name = forms.CharField(required=True, max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Mike','class':'form-control' , 'autocomplete': 'off','pattern':'[A-Za-z ]+', 'title':'Enter Characters Only '}))
    Middle_Name = forms.CharField(required=False, max_length=10)
    last_name = forms.CharField(required=True, max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Smith','class':'form-control' , 'autocomplete': 'off','pattern':'[A-Za-z ]+', 'title':'Enter Characters Only '}))
    sex = forms.ChoiceField(required=True, choices=SEX)


class GUARANTORForm(forms.Form):

    Patient_Relationship = forms.ChoiceField(required=True, choices=PTREL)
    guarantor_first_name = forms.CharField(required=True, max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Guarantor First Name','class':'form-control' , 'autocomplete': 'off','pattern':'[A-Za-z ]+', 'title':'Enter Characters Only '}))
    guarantor_last_name = forms.CharField(required=True, max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Guarantor Last Name','class':'form-control' , 'autocomplete': 'off','pattern':'[A-Za-z ]+', 'title':'Enter Characters Only '}))
    guarantor_sex = forms.ChoiceField(required=True,choices=SEX)

if Patient_Relationship = Self copy data from PDEM form fields to GUARANTOR form fields

1 个答案:

答案 0 :(得分:0)

我最终用JavaScript解决了这个问题

    <script type="text/javascript">
    document.querySelector("#id_guarantor-Patient_Relationship").onchange = function () {
        console.log(this.options[this.selectedIndex].value);
        if(this.options[this.selectedIndex].value == 'S'){
            document.querySelector("#id_guarantor-guarantor_first_name").value = document.querySelector("#id_patient-first_name").value
            document.querySelector("#id_guarantor-guarantor_last_name").value = document.querySelector("#id_patient-last_name").value
            document.querySelector("#id_guarantor-guarantor_sex").value = document.querySelector("#id_patient-sex").value

}
    }
</script>