0

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.

zinon
  • 4,427
  • 14
  • 70
  • 112
  • My head hurts just looking at that code. This isn't related to what you're asking, but wouldn't it make more sense to split things up a bit? Just for readability sake (and also maintainability and maybe extended behavior). You already have a few fields named `foo_fieldname`, so why not make a `Foo` class with those fields and link them instead of having one gigantic model with a million fields? – yuvi Oct 21 '14 at 13:38
  • @yuvi Yes, I'm really sorry! I did it. Regarding your last question, that's not in my responsibilities. I have to give a solution to this problem. – zinon Oct 21 '14 at 13:47
  • Well, for starters, you probably want to merge the two forms into one, then logic your way into saving the `patient`, and using the newly created object when saving the second one. – yuvi Oct 21 '14 at 13:55
  • wait a minute - `tab-pane`? Are you by any chance looking for a way to implement a step-by-step type of form, commonly known as a [Form Wizard](https://docs.djangoproject.com/en/1.4/ref/contrib/formtools/form-wizard/)? Also see my [answer here](http://stackoverflow.com/questions/19471344/how-to-implement-this-in-django-back-button-keep-data-in-request/19471481#19471481) – yuvi Oct 21 '14 at 14:00
  • @yuvi No, I do not want to implement formWizard. `tab-pane` is used for bootstrap template – zinon Oct 21 '14 at 14:13

1 Answers1

1

If you want an initial value you can try this:

form = MyForm(initial={'max_number': '3'})

You would need to replace the 3 by your patient id, which you need to figure how to pass it to the view. It is easy if you are trying something like:

my_demographic_object = my_demographic_form.save() #Object created from your old form
form = MyForm(initial={'patient': my_demographic_object.id})

If that is not what you are trying to do please explain a little more in detail

Community
  • 1
  • 1
cdvv7788
  • 2,021
  • 1
  • 18
  • 26
  • Thanks. When I use `form = MyForm(initial={'patient': my_demographic.id})` I get `'DemographicForm' object has no attribute 'id'` – zinon Oct 21 '14 at 14:24
  • You are using the form object, not the demographic object. Editing for clarity. – cdvv7788 Oct 21 '14 at 14:27
  • `my_demographics = DemographicForm(request.POST or None)` and next after the `my_demographics.save()` I have `mydiagnosis= DiagnosisForm(initial={'patient': my_demographics.patient_id})`. Isn't that ok? – zinon Oct 21 '14 at 14:31
  • I edited my answer. I am getting a reference to the demographics object from the my_demographics.save() line – cdvv7788 Oct 21 '14 at 14:32