I have the models:
class Patient(models.Model):
patientID = models.CharField(max_length=200 , 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 GeneralData(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
examination = models.CharField(max_length=100, choices=Examination_Choice, default='notchosenyet')
height = models.FloatField(help_text= '[m] not [cm]! ')
weight = models.FloatField(help_text= '[kg]')
And in my Website I navigate the User, after adding a patient, to the patient detail page. There, the User can add the GeneralData via an add-button. Now I want, that the CreateView of the GeneralData select automatically the patient. Because the user came to the CreateView from the patient detail page of one specific patient I think this should be possible. But I am totally new to Django and web development in general and have no clue how to fix my problem.
Thank you for your help!
P.S. Sorry for my english.
Edit:
My URLs:
# /member/<patient_id>/
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
# /member/generaldata/add/
url(r'patient/(?P<pk>[0-9]+)/generaldata/add/$', views.GeneralDataCreate.as_view(), name='generaldata-add'),
My Views:
class GeneralDataCreate(generic.CreateView):
model = GeneralData
fields = ['patient', 'examination', 'height', 'weight', 'aha_classification']
def get_initial(self):
patient = get_object_or_404(Patient.objects.get(pk=self.kwargs.get('pk')))
return {
'patientID': patient,
}