是否可以添加一個變量到Django Model Field.choices?是否可以添加一個變量到Django Model Field.choices?
當我必須靜態添加它時,我失去了功能。
IP_CHOICES = (
('192.168.1.0', '192.168.1.0'),
)
ip_address = models.IPAddressField(choices=IP_CHOICES, unique=True, blank=True)
我使用名爲IPy的Python IP解釋器來計算正確的IP塊。
ip = IP(self.network + slash)
for rangeip in enumerate(ip[2:-1]):
IP_CHOICES = (
("%s" %rangeip, "%s" %rangeip)
)
這可能嗎?如果是這樣,請幫助。過去一週試圖破解它,但沒有得到任何地方。任何幫助表示讚賞。
請查看模型類。
#IP Block Class
class IP_block(models.Model):
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 block and range save function
def save(self, *args, **kwargs):
slash = unicode(self.slash)
broadcast = IP(self.network + slash).broadcast()
subnet = IP(self.network+slash).strNetmask()
self.broadcast_ip = broadcast
self.subnet = subnet
ip = IP(self.network + slash)
for gateway in ip[1]:
self.gateway_ip = gateway
#rangeip for loop
ip = IP(self.network + slash)
if self.ip_range:
print 'no override'
else:
for rangeip in ip[2:-1]:
self.ip_range += "%s\n" %rangeip
IP_CHOICE = "(%s" %rangeip + ", %s)," %rangeip
#ip_list select
ip_list = models.CharField(choices=IP_CHOICE, max_length=128, blank=True)
super(IP_block, self).save(*args, **kwargs)
class Meta:
verbose_name_plural = 'IP Blocks'
def __unicode__(self):
return self.network
你得到的錯誤是什麼? 「IP(self.network + slash)'返回什麼? – karthikr
此外,你可以這樣做:'選擇=元組(zip(ip,ip))' – karthikr
ip = 192.168.1.0/27 - 這是ip塊。 – Infinixd