I have to models (demographic + diagnosis) and I've created 2 corresponding forms for these.
models.py
class demographic(models.Model):
national_health_care_pat_id = models.IntegerField('National Health Care patient id', null=True,blank=True)
patient_hospital_file_number = models.IntegerField(null=True,blank=True)
patient_id = models.IntegerField(unique= True ,primary_key=True)
def __str__(self):
return str(self.patient_id)
class diagnosis(models.Model):
patient = models.ForeignKey(demographic)
age_of_diagnosis = models.IntegerField(null=True,blank=True)
def __str__(self):
return str(self.patient_id)
views.py
def input(request):
form = DemographicForm(request.POST or None)
form_diag = DiagnosisForm(request.POST or None)
context = RequestContext(request)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
return render_to_response('test.html')
if form_diag.is_valid():
save_itd = form_diag.save(commit=False)
save_itd.save()
return render_to_response('test.html')
return render_to_response('input.html', {'frm':form, 'frm_d': form_diag}, context)
In my template input.html I have
<form class="tab-pane fade in active" id="demographics" method="post" >
{%crispy frm%}
</form>
<form class="tab-pane fade" id="diagnosis" method="post">
{%crispy frm_d%}
</form>
forms.py
class DemographicForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DemographicForm, self).__init__(*args, **kwargs)
self.helper=FormHelper(self)
self.fields['date_of_birth'].widget = widgets.AdminDateWidget()
self.helper.layout = Layout(
'national_health_care_pat_id',
'patient_hospital_file_number',
'patient_id',
FormActions(
Submit('submit', "Save changes"),
Submit('cancel', "Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = demographic
exclude = []
class DiagnosisForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DiagnosisForm, self).__init__(*args, **kwargs)
self.helper=FormHelper(self)
self.helper.layout = Layout(
'patient',
'age_of_diagnosis',
FormActions(
Submit('submit', "Save changes"),
Submit('cancel',"Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = diagnosis
exclude = []
Is there a way to correlate the demographic.patient_id with diagnosis.patient on the forms? I mean I fill and save the demographic form and next I fill the diagnosis form I want my form to have the demographic.patient_id value in diagnosis.patient without selecting it.