diff --git a/application/opa_mgnt/forms.py b/application/opa_mgnt/forms.py index 1ce6ab7..64eff58 100644 --- a/application/opa_mgnt/forms.py +++ b/application/opa_mgnt/forms.py @@ -1,21 +1,10 @@ from django import forms + +from common.forms import DepartmentSelectionForm from .models import * -from ..org_mgnt.models import PrimaryDepartment, SecondaryDepartment -class SocialMediaAccountRegistrationForm(forms.ModelForm): - primary_department = forms.ModelChoiceField( - queryset=PrimaryDepartment.objects.all(), - widget=forms.Select(attrs={'class': 'form-control'}), - empty_label="选择一级部门", - label="一级部门" - ) - secondary_department = forms.ModelChoiceField( - queryset=SecondaryDepartment.objects.none(), - widget=forms.Select(attrs={'class': 'form-control'}), - empty_label="选择二级部门", - label="二级部门" - ) +class SocialMediaAccountRegistrationForm(DepartmentSelectionForm, forms.ModelForm): class Meta: model = SocialMediaAccountRegistration @@ -30,14 +19,6 @@ class SocialMediaAccountRegistrationForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(SocialMediaAccountRegistrationForm, self).__init__(*args, **kwargs) - if 'primary_department' in self.data: - try: - primary_department_id = int(self.data.get('primary_department')) - self.fields['secondary_department'].queryset = SecondaryDepartment.objects.filter(primary_department_id=primary_department_id).order_by('secondary_department_name') - except (ValueError, TypeError): - pass - elif self.instance.pk: - self.fields['secondary_department'].queryset = self.instance.primary_department.secondarydepartment_set.order_by('secondary_department_name') class AccountOperationManagementForm(forms.ModelForm): diff --git a/common/forms.py b/common/forms.py new file mode 100644 index 0000000..2cce0e0 --- /dev/null +++ b/common/forms.py @@ -0,0 +1,31 @@ +from django import forms + +from application.org_mgnt.models import PrimaryDepartment, SecondaryDepartment + + +class DepartmentSelectionForm(forms.Form): + primary_department = forms.ModelChoiceField( + queryset=PrimaryDepartment.objects.all(), + widget=forms.Select(attrs={'class': 'form-control'}), + empty_label="选择一级部门", + label="一级部门" + ) + secondary_department = forms.ModelChoiceField( + queryset=SecondaryDepartment.objects.none(), + widget=forms.Select(attrs={'class': 'form-control'}), + empty_label="选择二级部门", + label="二级部门" + ) + + def __init__(self, *args, **kwargs): + super(DepartmentSelectionForm, self).__init__(*args, **kwargs) + if 'primary_department' in self.data: + try: + primary_department_id = int(self.data.get('primary_department')) + self.fields['secondary_department'].queryset = SecondaryDepartment.objects.filter(primary_department_id=primary_department_id).order_by('secondary_department_name') + except (ValueError, TypeError): + pass + elif 'instance' in kwargs and kwargs['instance']: + instance = kwargs['instance'] + if hasattr(instance, 'primary_department'): + self.fields['secondary_department'].queryset = instance.primary_department.secondarydepartment_set.order_by('secondary_department_name')