1👍
The main thing that you are not getting is on the models.py, so I will focus on it.
You need three tables to do what you have described: diagnosisData
, PatientData
and a ‘membership’ table which I call diagnosisPatient
. Then you build your model like this:
class diagnosisChoices(models.Model):
diagnosis = models.CharField(max_length = 50)
class PatientData(models.Model):
Name = models.CharField(max_length=100)
Surname = models.CharField(max_length=100)
dateOfBirth = models.DateField(default = datetime.datetime.now())
diagnosis = models.ManyToManyField('diagnosisChoices',through='diagnosisPatient')
class diagnosisPatient(models.Model):
patient = models.ForeignKey('PatientData')
diagnosis = models.ForeignKey('diagnosisChoices')
dateOfDiagnosis = models.DateField()
Once you have your model built this way, you should save your PatientData
and your diagnosisChoices
instances as usual. FOr the many to many relation, you should save it manualy on the diagnosisPatient
table using the apropriate foreign keys and date. You can query the many to many relation from the PatientData
model as usual with objects.all()
function.
The thing here to keep in mind is that ManyToMany relations in django are always creating a new membership table for you behind the scenes. So when you do not need to insert extra information on the relationship the diagnosisPatient
table is just made of two foreign keys, and it is hidden. The through argument on this relationship is just bringing this table to light and giving you control back to put whatever new relationship you like.