2010-06-29 4 views
0

저는 서버/데이터 센터 재고 관리 도구를 사용하고 있습니다. 나는 사용자 정의 장치 (리눅스 서버, 윈도우 서버, 라우터, 스위치 등)를 표현하는 데 사용되는 기본 "장치"를 정의하는 클래스를 가지고있다.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에서 클래스 이름을 지정해야합니다! –

답변

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에를 추가하는 것이 좋습니다 것입니다. 포트 (예 : 유선 또는 Wi-Fi)에 관계없이 하나의 주소로 호스트로 이동해야하는 경우가 아니면 기기에 둘 이상의 네트워크 포트가있을 수 있으므로 둘 이상의 IP 주소가있을 수 있습니다.

호스트 당 하나의 주소 만 보장하면 대신 dev = models.OneToOneField(device, related_name="address")을 원할 수 있습니다.

+0

+1 Fk 또는 OnetoOneKey가 적절합니다. OP가 GenericRelation을 사용하는 이유를 모르겠습니다. –

+0

OP가 1 인 이유를 모르겠습니다. 'device'에 여러 개의 이름 (서버의 경우가 많음)을 가질 수 있으므로'hostname'을'device'에 넣는 것이 좋습니다. 2)'장치'(하나만 구입할 수있을 것 같네요?), 또는 3) URL에'호스트 이름 '이나 나중에 필요할 때 변경할 수있는 다른 슬러그 대신'아이디'를 사용하여 보증 기간을 연장하십시오. –

+0

이 과정을 통해 처음 생각하는 스크래치 모델이기 때문입니다. 그러나 입력에 감사드립니다. – jduncan