【问题标题】:django model -- relationships between inherited models?django模型——继承模型之间的关系?
【发布时间】:2010-06-29 02:22:27
【问题描述】:

我正在开发一种服务器/数据中心库存管理工具。我有一个类来定义一个默认的“设备”,然后用它来表达自定义设备(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")

【问题讨论】:

  • 要遵循 Django 的编码约定,您应该始终以 UpperCamelCase 命名您的类!

标签: django orm django-models


【解决方案1】:

专注于手头的问题:

我的问题是,表达所有各种设备模型和 ipv4 地址模型之间关系的最佳方式是什么?

我建议将dev = models.ForeignKey(device, related_name="address")(也可能是portNumber = models.PositiveSmallIntegerField(default=0))添加到ipv4address。一台设备可以有多个网络端口,因此可以有多个 IP 地址,除非您有强制一个地址到主机而不考虑端口(例如有线或 wifi)。

如果您保证每个主机只有一个地址,那么您会想要dev = models.OneToOneField(device, related_name="address")。

【讨论】:

  • +1 Fk 或 OnetoOneKey 似乎合适。我不明白为什么 OP 使用 GenericRelation
  • 我不知道为什么 OP 是 1) 将 hostname 放入 device,因为 device 可能有多个名称(服务器通常是这种情况),2) 延长保修与device 一起使用(我猜你只能买一个?),或3)在URL 中使用id,而不是hostname 或其他一些可以稍后根据需要更改的slug。
  • 因为这是我在整个过程中思考的第一个临时模型。不过,感谢您的意见。
  • 迈克,遗憾的是,您无法定义与抽象类的外键关系。那是我尝试的第一件事。我认为 Django 中的通用前键方法是最好的选择,我只是在寻找替代的想法。 AssertionError: ForeignKey 无法定义与抽象类设备的关系
  • 是的,这就是我头疼的地方。将device 定义为抽象意味着您不能查询所有设备、IIRC……与使用更少的表相比,这似乎是一件相当有用的事情。无论如何,使用GenericRelation 关系而不是ForeignKey 看看效果如何。
猜你喜欢
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多