修复问题

1. 将一级部门和二级部门做得通用化
This commit is contained in:
王思川 2024-06-16 13:36:04 +08:00
parent 139fe1863f
commit 4dd45e6a02
2 changed files with 34 additions and 22 deletions

View File

@ -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):

31
common/forms.py Normal file
View File

@ -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')