0

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,
    }
Boerne
  • 149
  • 1
  • 16
  • Look here http://stackoverflow.com/questions/18277444/set-initial-value-in-createview-from-foreignkey-non-self-request-user – neverwalkaloner Jan 10 '17 at 10:11
  • Thank you! I edited my question. My URLs are without slugs and I cannot transfer the answer of the question you showed me to my problem. Can you help me with it? – Boerne Jan 10 '17 at 10:32
  • You should replace `slug` with `pk`: patient = Patient.objects.get(pk=self.kwargs.get('pk')) – neverwalkaloner Jan 10 '17 at 10:52
  • When I have done this I get the error:" IndentationError: unindent does not match any outer indentation level". And I think I have done something wrong when I say that " 'patientID':patient ".. Because it does not get the patientID, right? The Patient of the GeneralData Model is filled out with the patientID from the Patient model.. But this is just because of my "def __str__(self): return self.patientID". So if I get the object with the pk it should be possible to fill in the patientID for patient. – Boerne Jan 10 '17 at 11:08
  • You can read about Intentation error here: http://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level – neverwalkaloner Jan 10 '17 at 11:38
  • Thanks a lot! Had to write " 'patient' : patient" and the function were one space out of the class. Now it works! I want to upvote you but I do not have this privilege yet. – Boerne Jan 10 '17 at 13:21
  • You are welcome. Happy this helped. – neverwalkaloner Jan 10 '17 at 13:23

0 Answers0