class Patient(models.Model):
patientID = models.CharField(max_length=200, unique=True, help_text='Insert PatientID')
birth_date = models.DateField(auto_now=False, auto_now_add=False, help_text='YYYY-MM-DD')
gender = models.CharField(max_length=200,choices=Gender_Choice, default='UNDEFINED')
class Examination(models.Model):
number_of_examination = models.IntegerField()
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
date_of_examination = models.DateField(auto_now=False, auto_now_add=False, help_text='YYYY-MM-DD')
class GeneralData(models.Model):
examination = models.ForeignKey(Examination, on_delete=models.CASCADE)
height = models.FloatField(default='-', help_text= '[m] not [cm]! ')
weight = models.FloatField(default='-', help_text= '[kg]')
aha_classification = models.IntegerField(choices=AHA_CHOICES, default=0)
私の問題: 私は1人の特別な患者のための検査の数= 1で一般的なデータオブジェクトを照会する方法がわかりません。私は患者の詳細ページにオブジェクトを表示したい。私は問題なくExaminationクラスを照会できます。しかし、私はどのように一般的なデータオブジェクトをクエリするのか分からない。詳細ページには患者モデルのみが読み込まれます。このため、患者モデルから検査モデルを介してGeneraldataモデルに照会する必要がありますか?または、テンプレートに他のモデルをロードすることは可能ですか?ご協力いただきありがとうございます!
ありがとうございました! 「テンプレートに他のモデルをロードするために、詳細ページがロードのみ患者モデル(...)それは可能ですか?」
def DetailView(generic.DetailView):
model = Patient
template_name = 'app/detail.html'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(DetailView, self).get_context_data(**kwargs)
# Add in a QuerySet
context['FirstGeneral'] = GeneralData.objects.filter(examination__number_of_examination=1, examination__patient=get_object_or_404(Patient, pk=self.kwargs.get('pk')))
return context
で、一般的な
DetailView
を使用している場合は、あなたの質問を更新する代わりに、コメントでコードを投稿してください - と、より具体的に、「動作しない」ほとんど役に立たない可能性についてです問題の説明。また、あなたは明らかに現在の患者の '検査 'モデルをフィルタリングしたいと思う。 –