I am in the process of creating a database to hold quality inspection records and learning Django at the same time. I may have jumped into creating models too soon as I've run into conceptual problems of how I'm defining the classes. The goal is to have a "master" inspection plan that defines what is to be inspected and an "instance" inspection record where inspection data is recorded.
I created four classes, the important parts of which are defined as:
class qip_master_hdr(models.Model):
qip_number = models.CharField('QIP number',max_length=32)
qip_revision = models.CharField('QIP revision',max_length=4)
class Meta:
unique_together = ('qip_number', 'qip_revision')
verbose_name = 'QIP Master Header'
def __unicode__(self):
return 'QIP: ' + self.qip_number + ', REV: ' + self.qip_revision
class qip_master_dtl(models.Model):
qip_number_and_revision = models.ForeignKey(qip_master_hdr,related_name='qip_master_dtl')
dimension_number = models.PositiveIntegerField()
class Meta:
verbose_name = 'QIP Master Detail'
unique_together = ('qip_number_and_revision', 'dimension_number')
def __unicode__(self):
return str(self.qip_number_and_revision) + ', DIM: ' + str(self.dimension_number)
class qip_job_hdr(models.Model):
qip_number_and_revision = models.ForeignKey(qip_master_hdr,related_name='qip_job_hdr')
job_number = models.CharField(max_length=16)
class Meta:
verbose_name = 'QIP Job Header'
unique_together = ('qip_number_and_revision', 'job_number')
def __unicode__(self):
return str(self.qip_number_and_revision) + ', Job: ' + str(self.job_number)
class qip_job_dtl(models.Model):
## stuck here
In English, I want to create a master inspection plan. Each plan is defined by number and revision. Each plan has many dimensions on it. Once a master plan is fully defined with a number, revision, and at least one dimension, it can be used on a job. Each master plan can be used for many jobs. Each of those jobs has the same dimensions on them, but data is recorded for each of those dimensions.
One thing I considered is combining my header and detail classes and just make a master model and a job model. While that repeats a lot of data, it simplifies the model relationships. I don't know enough about performance to understand whether or not this is a bad idea. However, it removes the multiple foreign key relationship that is likely in qip_job_dtl.
The reason for the header and detail is that there are items on the header, such as product name, that don't need to be repeated for every single dimension on a plan or every single recorded dimension.
So, I'm stuck. I probably went too far without having a complete understanding of the schema I wanted to define. I'd like help with wrapping my head around what that definition should be based on my "in English" description. I think I can handle all of the mechanics of coding this if I can get to the point where I understand how to define the problem.
Thanks, Matt