2012-02-21 51 views
1

我在空閒時間學習Django(和編程)。這很有趣,但我在如何從邏輯上區分共享父級的兩個模型之間存在問題。適當的孩子類選擇

我想要做的是有一個窗體,我可以輸入一臺機器和一個文件來發送它。我遇到的問題是我有兩種類型的機器(它們是RS-232通信)。這些機器具有硬件流量控制或軟件流量控制。

我設置了一個url/transmitProgram /將輸入的表單數據發送到適當的地方。我不知道如何選擇正確的機器設置。

OfficeMachine是硬件流控制的機器和GarageMachine是軟件流控制

我在形式送送文件001 OfficeMachine(如何知道OfficeMachine是軟件或硬件)

Models.py

class SerialMachine(models.Model): 
    EVEN = 1 
    ODD = 2 
    NONE = 3 
    PARITY_CHOICES = (
     (EVEN, "Even"), 
     (ODD, "Odd"), 
     (NONE, "None"), 
    ) 

    machineName = models.CharField(max_length=50) 
    address = models.CharField(max_length=100) 
    baudRate = models.IntegerField(max_length=50) 
    parity = models.IntegerField(max_length=1, choices=PARITY_CHOICES) 
    dataBits = models.IntegerField(max_length=15) 
    stopBits = models.IntegerField(max_length=15) 
    carriageReturn = models.BooleanField(default=False) 
    lineFeed = models.BooleanField(default=True) 
    endProgramChar = models.BooleanField(default=True) 
    machineServer = models.ForeignKey('SerialPortServer') 
    repository = models.ForeignKey('Repository') 

    class Meta: 
     abstract = True 

    def __unicode__(self): 
     return self.machineName 

class HardwareFlowControlMachine(SerialMachine): 
    """ 
    Represents all CNC machines that are to be connected for a hardware flow 
    control connection 
    """ 
    enableRTSCTS = models.BooleanField(default=False) # RTS/CTS Flow Control 
    enableDSRDTR = models.BooleanField(default=True) # DSR/DTR Flow Control 

class SoftwareFlowControlMachine(SerialMachine): 
    """" 
    Represents all CNC machines that are to be connected for a software flow 
    control connection 
    """ 
    xonChar = models.IntegerField(max_length=2, default=17) 
    xoffChar = models.IntegerField(max_length=2, default=19) 

我的視圖從窗體中獲取數據並調用幫助函數來獲取路徑和機器設置。

Helper.py

def getMachineSettings(machine): 
    from django.core.exceptions import ObjectDoesNotExist 

    from src.apps.cnc.models import SoftwareFlowControlMachine, HardwareFlowControlMachine 

    machineSettings = "" 
    try: 
     machineSettings = SoftwareFlowControlMachine.get(name_iexact='%s' % machine) 
    except ObjectDoesNotExist: 
     pass 
    if machineSettings == "": 
     try: 
      machineSettings = HardwareFlowControlMachine.get(name_iexact='%s' % machine) 
     except ObjectDoesNotExist: 
      pass 
    return machineSettings 

這似乎只是一個愚蠢的方式做到這一點,雖然。這篇文章(link)向我介紹了djangosnippets網站。

我在看着this snippet,但我看到一切都很融洽。

我是在理解是正確的,如果我加入我的SerialMachine absract類繼承ParentModel並創建一個ChildManager我可以只說

machineSettings = SerialMachine.get(name_iexact'%s' % machine) 
在繼承的類

和領域會在那裏等我嗎?

非常感謝你的幫助和信息,你可以爲我

+2

不要稱之爲「助手」稱它爲「工廠」人們更喜歡「工廠」設計模式;它使工作意味着它創建子類對象 – 2012-02-21 10:42:01

+0

謝謝,先生,我會記住 – Dan 2012-02-21 21:27:32

+0

不記得它瞭解決你的問題 – 2012-02-21 22:03:37

回答

0

根據您的需求的宏大範圍,可能需要第三方軟件包,如@yedpodtrzitko建議。然而,這可以通過利用Django處理MTI(多表繼承)的方式簡單地完成,無需額外的機器。 SerialMachine的每個實例都將具有一個以任何子類用於創建它的名稱命名的屬性。在此基礎上,你可以通過簡單的屬性的存在測試獲得從父的子類:

class SerialMachine(models.Model): 
    ... 
    @property 
    def child(self): 
     if hasattr(self, 'hardwareflowcontrolmachine'): 
      return self.hardwareflowcontrolmachine 
     elif hasattr(self, 'softwareflowcontrolmachine'): 
      return self.softwareflowcontrolmachine 
     else: 
      return None 

然後,只需使用machine.child無論你需要的特定機型。 (你當然可以改變屬性的名稱爲你喜歡的任何東西

+0

非常感謝,這看起來比我更優雅曾嘗試過 – Dan 2012-02-21 21:28:27