I am learning Django (and programming) in my free time. It is fun but I am having an issue with how to logically make a distinction between two models that share a parent.
What I would like to do is have a form where I can enter a machine and a file to send it. The issue I have is that I have two types of machines (they are RS-232 communication) These machines have either hardware flow control or software flow control.
I have set a url /transmitProgram/ to take entered form data and send it to the proper place. I do not know how to select the right machine settings.
OfficeMachine is a hardware flow controlled machine and GarageMachine is software flow controlled
I send in my form to send file 001 to OfficeMachine (how to know if OfficeMachine is software or hardware)
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)
My view takes the data from the form and calls a helper function to get the path and machine settings.
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
This just seems like a dumb way to do it though. This post (link) introduced me to the djangosnippets website.
I was looking at this snippet but I am pretty weak in seeing everything fit together.
Am I correct in understanding that if I add inherit ParentModel in my SerialMachine absract class and create a ChildManager that I could just say
machineSettings = SerialMachine.get(name_iexact'%s' % machine)
and from the fields in the inherited classes will be there for me?
Thanks a lot for help and info you can provide for me
See InheritanceManager in django-model-utils, maybe it will fits your needs.