无法根据动态下拉列表选择数据。 保存用户数据并填充为列表。
夫妇问题:
如何将数据从AddData
带入SiteData
同样,当用户从code
中选择下拉列表SiteData
时,如何仅为该SiteData
关联或填充数据
如何使用site_ip,site_prefix,site_data1,site_data2映射数据code
还注意到,当选择下拉菜单时,我得到的值是数字而不是文本名称。
例如:
网站68
site_ip 1
模型
class Code (models.Model):
name = models.CharField(max_length=4, default=None, blank=True, unique=True, error_messages={'unique':"Already in Used"})
def __str__(self):
return self.name
class AddData (models.Model):
code = models.ForeignKey(Code, on_delete=models.CASCADE, null=True)
data1_ip = models.GenericIPAddressField(protocol='IPv4', unique=True, error_messages={'unique':"Already in Used"})
data2_prefix = models.CharField(max_length=2, default='22')
def __str__(self):
return self.data1_ip
class SiteData (models.Model):
site = models.ForeignKey(Code, on_delete=models.CASCADE, null=True)
site_ip = models.ForeignKey(AddData, on_delete=models.CASCADE, null=True)
site_prefix = models.ForeignKey(AddData, on_delete=models.CASCADE, null=True)
site_data1 = models.CharField(max_length=3, default='120')
site_data2 = models.CharField(max_length=1, default='1')
表格
class CODE_Form(forms.ModelForm):
class Meta:
model = Code
fields = ('name',)
class AddData_Form(forms.ModelForm):
class Meta:
model = MIDTIER_Add_Site
fields = ('code', 'data1_ip', 'data2_prefix',)
class SiteData_Form(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(SiteData_Form, self).__init__(*args, **kwargs)
if 'site' in self.data:
c = self.data.get('site')
c_ip = self.data.get('site_ip')
self.fields['site'] = forms.ModelChoiceField(queryset=CODE.objects.filter(name=c))
self.fields['site_ip'] = forms.ModelChoiceField(queryset=CODE.objects.filter(name=c_ip))
class Meta:
model = AddData
fields = '__all__'
field_classes = { 'json_field': JSONFormField, }
谢谢您的帮助。