当文本字段中的 IP 列表进入“ip_list_coices”时,我遇到了一个问题,而不是将每个 IP 作为一个选项放在一行中。
IP 需要是单独的选择。我有一种感觉,这可能是一个简单的修复,但似乎无法弄清楚。
以下是示例:
这就是发生的事情:
192.168.1.2 192.168.1.3 ... 192.168.1.5
这就是我所希望的:
192.168.1.2
192.168.1.3
...
192.168.1.5
模型.py
#IP Block Class class IP_block(models.Model): #ip block and range save function def save(self, *args, **kwargs): slash = unicode(self.slash) self.broadcast_ip = broadcast self.subnet = subnet #rangeip for loop ip = IP(self.network + slash) for rangeip in ip[2:-1]: self.ip_range += "%s \n" %rangeip super(IP_block, self).save(*args, **kwargs) network = models.IPAddressField(unique=True) slash = models.ForeignKey(Subnet, verbose_name='CIDR') subnet = models.CharField(max_length=64, blank=True) gateway_ip = models.CharField(max_length=64, blank=True) broadcast_ip = models.CharField(max_length=64, blank=True) ip_range = models.TextField(blank=True, verbose_name='Available IP Range') dslam = models.ManyToManyField(Dslam, verbose_name='Dslam', blank=True) ip_list = models.CharField(max_length=128, blank=True) class Meta: verbose_name_plural = 'IP Blocks' def __unicode__(self): return self.network
丹尼尔推荐的forms.py
class IP_blockForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(IP_blockForm, self).__init__(*args, **kwargs) if self.instance and self.instance.ip_range: #This is where I pass the list from the text field ip_list_choices = [(self.instance.ip_range, self.instance.ip_range ),] self.fields['ip_list'] = forms.ChoiceField(choices=ip_list_choices) class Meta: model = IP_block