so I have this two models:
class Patient(models.Model):
patientID = models.CharField(max_length=200 , default='Enter PatientID')
age = models.IntegerField(default='-')
gender = models.CharField(max_length=200,choices=Gender_Choice, default='UNDEFINED')
class RiskFactor(models.Model):
patient = models.OneToOneField(Patient, on_delete=models.CASCADE)
hypertension = models.BooleanField(default=False)
diabetes = models.BooleanField(default=False)
PAVK = models.BooleanField(default=False)
nicotin = models.BooleanField(default=False)
So I've changed from ForeignKey to OnetoOneField because of reasons. So with the ForeignKey everything was easypeasy and I can show the data of the RiskFactor model in my Patient detail template. Now I have problems to change the query. I just don't know how to change this:
<ul> {% for rfac in patient.riskfactor_set.all %} <li>Hypertension: {{ rfac.hypertension }}<br/> Diabetes: {{ rfac.diabetes }}<br/> PAVK: {{ rfac.PAVK }}<br/> Nicotin: {{ rfac.nicotin }}<br/> </li> {% endfor %} </ul>
into sth that shows the riskfactor data from the patient with a OneToOneRel.
Thanks for your help!
I would add related_name="risk_factor"
to your OneToOneField declaration, just to make it more clear how to reference the associated RiskFactor. From there, you can reference it like any other property of patient
- it no longer is a list, but a single object. Like so below
<ul>
{% with patient.risk_factor as rfac %}
<li>Hypertension: {{ rfac.hypertension }}<br/>
Diabetes: {{ rfac.diabetes }}<br/>
PAVK: {{ rfac.PAVK }}<br/>
Nicotin: {{ rfac.nicotin }}<br/>
</li>
{% endwidth %}
</ul>
You can do without the with
statement, but it makes it easiest to convert your code, plus then you would have to put patient.risk_factor.
before every property you want, so might as well save it to rfac
.
Your trying to query a set of a one to one relationship which isn't possible. Since every Patient can only have 1 RiskFactor.
Why don't you get the risk factors based on the patient and pass it into the template like this:
patient_risk = RiskFactor.objects.get(patient=patient)
ctx = {
'rfac': patient_risk
}
Then in your template you could access the risks for that particular patients.
<ul>
<li>Hypertension: {{ rfac.hypertension }}<br/>
Diabetes: {{ rfac.diabetes }}<br/>
PAVK: {{ rfac.PAVK }}<br/>
Nicotin: {{ rfac.nicotin }}<br/>
</li>
</ul>
You can also query all of the RiskFactors models and access the patient via RiskFactor.patient
if you want to loop over all patients who have RiskFactors.
all_risk_factors = RiskFactor.objects.all()
ctx = {
'risk_factors': all_risk_factors
}
<ul>
{% for risk in risk_factors %}
<li><strong>Patient ID: risk.patient.patientID</strong></li>
<li>Hypertension: {{ risk.hypertension }}<br/>
Diabetes: {{ risk.diabetes }}<br/>
PAVK: {{ risk.PAVK }}<br/>
Nicotin: {{ risk.nicotin }}<br/>
</li>
{% endfor %}
</ul>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.