Best method to model django database using through table to access associations by date
Looking for advice on a tidy solution for accessing
The end result I am looking for is the following report:
cast, start_date, wire, winch
I currently run a script when a cast object is submitted via form to get the active wire associated with the cast, and save it in the 'wire' foreign key field. The active wire is accessed in the script through property tags. Technically this information is retained in the database through a series of models associating a cast with a wire (Cast-DrumLocation-WireDrum), and the date fields that store when the events occurred. These events update the association between cast and wire.
Is the way I modelled this a proper solution?, or is there another solutions where the wire foreign key does not need to be stored as a field in the cast model.
class Cast(models.Model):
id = models.AutoField(db_column='Id', primary_key=True)
startdate = models.DateTimeField(db_column='StartDate')
enddate = models.DateTimeField(db_column='EndDate')
wire = models.ForeignKey('Wire', models.DO_NOTHING, db_column='WireId')
winch = models.ForeignKey('Winch', models.DO_NOTHING, db_column='WinchId')
class Drum(models.Model):
id = models.AutoField(db_column='Id', primary_key=True\)
location = models.ManyToManyField(Location, through='DrumLocation', related_name='active_location')
material = models.TextField(db_column='Material')
wiretype = models.TextField(db_column='WireType')
class Winch(models.Model):
id = models.AutoField(db_column='Id', primary_key=True)
name = models.TextField(db_column='Name')
drums = models.ManyToManyField(Drum, through='Drumlocation', related_name='active_drum')
class Wire(models.Model):
id = models.AutoField(db_column='Id', primary_key=True)
nsfid = models.TextField(db_column='NsfId')
drums = models.ManyToManyField(Drum, through='WireDrum', related_name='loaded_wires')
class DrumLocation(models.Model):
id = models.AutoField(db_column='Id', primary_key=True)
date= models.DateField(db_column='Date')
drumid = models.ForeignKey(Drum, models.DO_NOTHING, db_column='DrumId')
winch = models.ForeignKey(Winch, models.DO_NOTHING, db_column='WinchId')
location = models.ForeignKey(Location, models.DO_NOTHING, db_column='LocationId')
wire = models.ForeignKey(Wire, models.DO_NOTHING, db_column='WireId')
class Wiredrum(models.Model):
id = models.AutoField(db_column='Id', primary_key=True, blank=True, null=False)
drum = models.ForeignKey(Drum, models.DO_NOTHING, db_column='DrumId', related_name='reverse_drum')
wire = models.ForeignKey(Wire, models.DO_NOTHING, db_column='WireId')
date = models.DateField(db_column='Date')