我正在研究服务器/数据中心库存管理工具。我有一个类来定义一个默认的“设备”,然后用它来表达自定义设备(Linux服务器,Windows服务器,路由器,交换机等)。
我还设置了数据模型来表达网络中的IP地址。
我的问题是,表达所有各种设备模型和ipv4地址模型之间关系的最佳方式是什么?
class device(models.Model):
'''the primary object. all types of networked devices are based on and inherit this class'''
STATUS_CHOICES = (('0', 'unknown'),('1','active'),('2','pending'),('3','inactive'),('4', 'inventory'),('5','mothballed'),('6','surplus'),)
'''technical information'''
hostname = models.CharField(max_length=45, unique=True, db_index=True, help_text="The published hostname for this device")
'''misc. information'''
description = models.TextField(blank=True, null=True, help_text="A free-form description field")
type = models.ForeignKey(deviceStatus, help_text="The classification for this device")
status = models.IntegerField(choices=STATUS_CHOICES, help_text="current status of device")
is_monitored = models.BooleanField(default=True, help_text="is the device monitored?")
date_added = models.DateTimeField(auto_now=True, help_text="Date and Time device is entered into Barrelhouse", editable=False)
warranty_expriry = models.DateField(help_text="Date Manufacturer warranty expires")
extended_warranty = models.BooleanField(help_text="Whether or not device has some extended warranty in addition to the manufacturer",default=False, validators=[validate_extended_warr])
ext_warranty_expiry = models.DateField(help_text="Date Extended Warranty Expires", null=True)
account = models.ForeignKey(vendorAccount, help_text="account associated with this device")
class Meta:
abstract = True
class deviceLinuxSystem(device):
'''a typcial linux system --- you can get as specific as you want to in various server and desktop types.'''
ip_address = generic.GenericRelation(ipv4address)
def get_absolute_url(self):
return "linux_devices/%i/" % self.id
def __unicode__(self):
return self.hostname
class Meta:
verbose_name = "Linux System"
class deviceWindowsSystem(device):
'''a typical windws system'''
def get_absolute_url(self):
return "windows_devices/%i/" % self.id
def __unicode__(self):
return self.hostname
class Meta:
verbose_name = "Windows System"
class ipv4address(models.Model):
'''a model to represent all used IPv4 addresses on networks'''
netIP = models.IPAddressField(help_text="associated address", verbose_name="IP Address", unique=True, db_index=True)
network = models.ForeignKey(network, help_text="Network this IP lives on")
答案 0 :(得分:1)
专注于手头的问题:
我的问题是,表达所有各种设备模型和ipv4地址模型之间关系的最佳方式是什么?
我建议将dev = models.ForeignKey(device, related_name="address")
(可能还有portNumber = models.PositiveSmallIntegerField(default=0)
)添加到ipv4address
。设备可以有多个网络端口,因此可以有多个IP地址,除非您有一些强制执行一个地址的东西,无论端口是什么(例如有线或无线)。
如果您保证每个主机只有一个地址,那么您需要dev = models.OneToOneField(device, related_name="address")
代替。