当我的文本字段中的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
models.py
根据Daniel 的推荐#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
答案 0 :(得分:1)
您可以这样做:
split_range = self.instance.ip_range.split(' ') #Or whatever your delimiter is
ip_list_choices = zip(split_range, split_range) #Gives you a tuple.
self.fields['ip_list'] = forms.ChoiceField(choices=ip_list_choices)