2010-06-29 65 views
0

我正在研究服務器/數據中心庫存管理工具。我有一個類來定義默認的「設備」,然後用它來表示自定義設備(Linux服務器,Windows服務器,路由器,交換機等)django模型 - 繼承模型之間的關係?

我也有數據模型設置爲表示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

要遵循Django的編碼約定,您應該始終將您的類命名爲UpperCamelCase! – 2010-06-29 09:16:25

回答

1

手頭關注的問題是:

My question is, what would be the best way to express the relationship between all of the various device models and the ipv4 address model?

我建議增加dev = models.ForeignKey(device, related_name="address")(也可能是portNumber = models.PositiveSmallIntegerField(default=0))到ipv4address。一個設備可以有多個網絡端口,因此可以有多個IP地址,除非你有一些東西強制執行一個地址去主機而不管端口(例如有線或無線)。

如果你保證每個主機只有一個地址,那麼你應該想要dev = models.OneToOneField(device, related_name="address")

+0

+1 Fk或OnetoOneKey似乎很合適。我不明白爲什麼OP使用GenericRelation – 2010-06-29 08:30:34

+0

我不知道爲什麼OP是1)將'hostname'放在'device'中,因爲'device'可能有多個名稱(通常是服務器的情況), 2)使用'device'(我猜你只能購買一個?)擴展保證,或者3)在URL中使用'id',而不是'hostname'或其他可以在以後需要時更改的slug。 – 2010-06-29 11:57:07

+0

,因爲這是我認爲通過這個過程的第一個劃痕模型。但是,感謝您的意見。 – jduncan 2010-06-29 13:53:03