Home > database >  Django: How to get value in one model from the other model?
Django: How to get value in one model from the other model?

Time:07-07

It feels like a very simple question, but how to get value in one model from the other one in django?

I have my models.py like this:

class Patient(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='Patient ID')

class Salvage_Treatment(models.Model):
    id = models.AutoField(primary_key=True)
    id_patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    Salvage_treatment_end_date = models.DateField()

class Survival_Parameters(models.Model):
    id = models.AutoField(primary_key=True)
    salv_treat = models.ForeignKey(Salvage_Treatment, on_delete=models.CASCADE)
    salvage_treatment_end_date = models.DateField()
    id_patient = models.ForeignKey('Patient', on_delete=models.CASCADE)

Let's assume that I have a patient with id_patient = 2 and I fullfiled the Salvage_Treatment form for him with id=1 (id of this form) and some date in Salvage_treatment_end_date. Now I need to have this same date to be put automatically in Survival_Parameters.salvage_treatment_end_date. How can I do that? I don't want to choose which Salvage_Treatment form to get the data from - instead I ONLY want to select the patient's ID.

CodePudding user response:

Not sure which of the following three is the query you asking for

salvage_treatment_end_date = Salvage_Treatment.objects.filter(id_patient=patient_id_you_have).values(‘Salvage_treatment_end_date’)
salvage_treatment_end_date = Survival_Parameters.objects.filter(id_patient=patient_id_you_have).values(‘salvage_treatment_end_date’)
salvage_treatment_end_date = Survival_Parameters.objects.filter(salv_treat__id_patient=patient_id_you_have).values(‘salvage_treatment_end_date’)

The .filter method restrict queryset by the patient’s id you have, and the .values method restrict output to a single field.

Note the double _ after salv_treat in the third query, that’s django’s way of referring relational field.

I could agree no more with @nigel222, even if you use id_patient instead of patient_id. When you access salvage_treatment.patient_id and it actually returns a Patient instance, it strongly hurts readability.

CodePudding user response:

as i assume you don't want to set default value of current date of salvage_treatment_end_date for Survival_Parameters default=datetime.now

but in your case you can overwrite save() method of Survival_Parameters and here you can save date of Salvage_treatment_end_date in you salvage_treatment_end_date just like this

class Patient(models.Model):
id = models.AutoField(primary_key=True, verbose_name='Patient ID')

class Salvage_Treatment(models.Model):
    id = models.AutoField(primary_key=True)
    id_patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    Salvage_treatment_end_date = models.DateField()

class Survival_Parameters(models.Model):
    id = models.AutoField(primary_key=True)
    salv_treat = models.ForeignKey(Salvage_Treatment, 
on_delete=models.CASCADE)
    salvage_treatment_end_date = models.DateField()
    id_patient = models.ForeignKey('Patient', on_delete=models.CASCADE)

    def save(self, **kwargs):
        self.salvage_treatment_end_date = 
self.salv_treat.Salvage_treatment_end_date
        super().save(**kwargs)

becasue of the relation (one to many) between these two table you can access the Salvage_Treatment with self.salv_treat and also access the Salvage_treatment_end_date with self.salv_treat.Salvage_treatment_end_date after all you call the call super class save() method

  • Related