我正在使用通用UpdateView编辑表单。当我打开表单时,无法获取保存的值以显示在select标记中,其他字段正常工作。
models.py
class employee_master(models.Model):
name = models.CharField(max_length=200)
for_station = models.CharField(max_length=200)
contact_no = models.CharField(max_length=200)
email_id = models.EmailField(max_length=200)
joining_date = models.DateField(max_length=200)
date_of_birth = models.DateField(max_length=200)
address = models.TextField(max_length=200)
country = models.CharField(max_length=200)
state = models.CharField(max_length=200)
city = models.CharField(max_length=200)
pin_code = models.CharField(max_length=200)
remarks = models.CharField(max_length=200)
active = models.BooleanField(default=False)
def get_absolute_url(self):
return reverse('master:employee-detail')
def __str__(self):
return self.name
forms.py
class EmployeeForm(forms.ModelForm):
city = forms.ModelChoiceField(queryset=city_master.objects.all())
state= forms.ModelChoiceField(queryset=state_master.objects.all())
country = forms.ModelChoiceField(queryset=country_master.objects.all())
class Meta:
model=employee_master
fields = '__all__'
widgets= {
'name':forms.TextInput(attrs={'class':'form-control'}),
'for_station': forms.TextInput(attrs={'class': 'form-control'}),
'contact_no': forms.TextInput(attrs={'class': 'form-control'}),
'email_id': forms.TextInput(attrs={'class': 'form-control'}),
'joining_date': forms.TextInput(attrs={'class': 'form-control'}),
'date_of_birth': forms.TextInput(attrs={'class': 'form-control'}),
'address': forms.Textarea(attrs={'class': 'form-control','rows':2, 'cols':10}),
'country': forms.Select(attrs={'class': 'form-control'}),
'state': forms.Select(attrs={'class': 'form-control'}),
'city': forms.Select(attrs={'class': 'form-control'}),
'pin_code': forms.TextInput(attrs={'class': 'form-control'}),
'remarks': forms.Textarea(attrs={'class': 'form-control','rows':2, 'cols':10}),
'active': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
}
views.py创建和更新,我使用的是同一表格
class EmployeeCreate(CreateView):
model = employee_master
form_class = EmployeeForm
class EmployeeUpdate(UpdateView):
model = employee_master
form_class = EmployeeForm
我希望在编辑时会显示国家,州和城市保存的值。