如何将值从mysql传递到create视图中的表单域

时间:2018-03-05 09:53:55

标签: django python-2.7 django-forms django-templates django-views

我有一个带有创建的createview类的PatientConsultationView,以及一个列出所有咨询的AllConsultationView。我想将咨询ID传递给PatientObservationView并使用它来填充PatientObservationForm OR,只是将其保存在PatientObservation模型中,而最终用户无法手动输入ID,因为它可以从AllConsultationView获取

views.py

class PatientConsultationView(CreateView):
    model = PatientConsultation
    template_name = 'patient/patient_consultation_add.html'
    form_class = PatientConsultationForm
    context_object_name = 'Patient_Consultation'
    success_url = reverse_lazy('patient_list')

class AllConsultationView(ListView):
    model = PatientConsultation
    template_name = 'patient/all_consultation.html'
    context_object_name = 'All_Patient_Consultation'

    def get_consult_id(self):

        return PatientConsultation.objects.filter(consultation_id=self)

class PatientObservationView(CreateView):
    model = PatientObservation
    template_name = 'patient/patient_observation_add.html'
    form_class = PatientObservationForm
    context_object_name = 'Patient_Observation'
    success_url = reverse_lazy('patient_list')

forms.py

class PatientConsultationForm(forms.ModelForm):

    class Meta:

        model = PatientConsultation
        fields = ('patient_id', 'weight', 'temperature', 'blood_pressure', 'pulse', 'oxygen_saturation',
                  'body_mass_index', 'status',)

        widgets = {
            'patient_id': forms.Select(attrs={'class': 'form-control input-height', 'placeholder': 'Patient ID'}),
            'weight': forms.TextInput(attrs={'class': 'form-control input-height', 'placeholder': 'Patient Weight'}),
            'temperature': forms.TextInput(attrs={'class': 'form-control input-height', 'placeholder': 'Patient Temperature'}),
            'blood_pressure': forms.TextInput(attrs={'class': 'form-control input-height', 'placeholder': 'Patient Blood Pressure'}),
            'pulse': forms.TextInput(attrs={'class': 'form-control input-height', 'placeholder': 'Patient Pulse'}),
            'oxygen_saturation': forms.TextInput(attrs={'class': 'form-control input-height ', 'placeholder': 'Patient Oxygen Saturation'}),
            'body_mass_index': forms.TextInput(attrs={'class': 'form-control input-height', 'placeholder': 'Patient Body Mass Index'}),
            'status': forms.Select(attrs={'class': 'form-control input-height', 'placeholder': 'Patient Status',}),
        }


class PatientObservationForm(forms.ModelForm):

    class Meta:

        model = PatientObservation
        fields = {'consultation_id','note_to_laboratories','note_to_pharmacist','note_to_physiotherapist',
                  'note_to_radiologist','medical_advice','medical_report','referral_note'}


        widgets = {
            'consultation_id': forms.TextInput(attrs={'class': 'form-control input-height', 'placeholder': 'Patient ID'}),
            'note_to_laboratories': RichTextFormField(),
            'note_to_pharmacist': RichTextFormField(),
            'note_to_physiotherapist': RichTextFormField(),
            'note_to_radiologist': RichTextFormField(),
            'medical_advice': RichTextFormField(),
            'medical_report': RichTextFormField(),
            'referral_note': RichTextFormField(),

models.py

class PatientConsultation(models.Model):
    """
    Model representing Observation records made by nurses
    """
    PATIENT_CONSULTATION_STATUS = (
        ('1', 'Consulting'),
        ('0', 'Done')
    )

    consultation_id = models.AutoField(primary_key=True, editable=False)
    patient_id = models.ForeignKey(PatientProfile)
    weight = models.CharField(max_length=5)
    temperature = models.CharField(max_length=5)
    blood_pressure = models.CharField(max_length=5)
    pulse = models.CharField(max_length=50)
    oxygen_saturation = models.CharField(max_length=50)
    body_mass_index = models.CharField(max_length=50)
    status = models.CharField(max_length=50, choices=PATIENT_CONSULTATION_STATUS, default='1')
    date_added = models.DateTimeField(auto_now_add=True)


    def __str__(self):
        """
        String for representing the model object (in Admin)
        """
        return str(self.consultation_id)


class PatientObservation(models.Model):
    """
    Model representing doctors report of a particular episode
    """
    consultation_id = models.ForeignKey(PatientConsultation)
    note_to_pharmacist = RichTextField(blank=True, null=True)
    note_to_laboratories = RichTextField(blank=True, null=True)
    note_to_physiotherapist = RichTextField(blank=True, null=True)
    note_to_radiologist = RichTextField(blank=True, null=True)
    referral_note = RichTextField(blank=True, null=True)
    medical_report = RichTextField(blank=True, null=True)
    medical_advice = RichTextField(blank=True, null=True)

    def __str__(self):
        """
        String for representing the model object (in Admin)
        """
        return self.consultation_id

My PatientConsultationAdd.html template formTag

<form method="POST" class="form-horizontal" id="submit_form"  action="#" enctype="multipart/form-data">
                    {% csrf_token %}
                        <div class="form-wizard">
                            <div class="form-body">
                                <div class="form-group">
                                    <label class="control-label col-md-3" for="">Select Consultation ID
                                    <span class="required"> * </span> </label>
                                    <div class="col-md-6">
                                        {{ form.consultation_id }}
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-md-3" for="">Note To Laboratories
                                    <span class="required"> * </span></label>
                                    <div class="col-md-6">
                                        {{ form.note_to_laboratories }}
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-md-3" for="">Note To Pharmacist
                                    <span class="required"> * </span></label>
                                    <div class="col-md-6">
                                        {{ form.note_to_pharmacist }}
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-md-3" for="">Note To Physiotherapist
                                    <span class="required"> * </span></label>
                                    <div class="col-md-6">
                                        {{ form.note_to_physiotherapist }}
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-md-3" for="">Note To Radiologist
                                    <span class="required"> * </span></label>
                                    <div class="col-md-6">
                                        {{ form.note_to_radiologist }}
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-md-3" for="">Medical Advice
                                    <span class="required"> * </span></label>
                                    <div class="col-md-6">
                                        {{ form.medical_advice }}
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-md-3" for="">Medical Report
                                    <span class="required"> * </span></label>
                                    <div class="col-md-6">
                                        {{ form.medical_report }}
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-md-3" for="">Referral Note
                                    <span class="required"> * </span></label>
                                    <div class="col-md-6">
                                        {{ form.referral_note }}
                                    </div>
                                </div>
                                <div class="form-actions">
                                    <div class="row">
                                        <div class="col-md-offset-3 col-md-9">
                                            <button type="submit" class="btn btn-primary">Save</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^patient/$', views.PatientList.as_view(), name='patient_list'),
    url(r'^patient/new/$', views.PatientCreate.as_view(), name='patient_new'),
    url(r'^patient/detail/(?P<patient_id>[0-9A-Fa-f-]+)/$', views.PatientDetails.as_view(), name='patient_details'),
    url(r'^patient/update/(?P<patient_id>[0-9A-Fa-f-]+)/$', views.PatientUpdate.as_view(), name='patient_update'),
    url(r'^patient/delete/(?P<patient_id>[0-9A-Fa-f-]+)/$', views.PatientDelete.as_view(), name='patient_delete'),
    url(r'^patient/family-add/$', views.FamilyMemberAdd.as_view(), name='family_add'),
    url(r'^patient/family-update/(?P<patient_id>[0-9A-Fa-f-]+)/(?P<family_member_id>[0-9]+)/$', views.FamilyMemberUpdate.as_view(), name='family_update'),
    url(r'^patient/family-delete/(?P<patient_id>[0-9A-Fa-f-]+)/(?P<family_member_id>[0-9]+)/$', views.FamilyMemberDelete.as_view(), name='family_delete'),
    url(r'^patient/family-detail/(?P<patient_id>[0-9A-Fa-f-]+)/(?P<family_member_id>[0-9]+)/$', views.FamilyMemberDetails.as_view(), name='family_detail'),
    url(r'^patient/new-consultation/(?P<patient_id>[0-9A-Fa-f-]+)/$', views.PatientConsultationView.as_view(), name='patient_consultation'),
    url(r'^patient/all-consultation/$', views.AllConsultationView.as_view(), name='all_patient_consultation'),
    url(r'^patient/new-observation/(?P<consultation_id>[0-9]+)/(?P<patient_id>[0-9A-Fa-f-]+)/$', views.PatientObservationView.as_view(), name='patient_observation'),

]

1 个答案:

答案 0 :(得分:1)

您可以使用Models and request.user下的modelform文档中描述的技术来设置请求kwargs中的咨询ID。

class PatientObservationView(CreateView):
    ...
    def form_valid(self, form):
        form.instance.consultation_id_id = self.kwargs['consultation_id']
        return super(PatientObservationView, self).form_valid(form)

(这里稍微愚蠢的属性名称是因为你调用了ForeignKey字段consultation_id而不是consultation,所以底层的db列是consultation_id_id。你真的不应该这样做。)

另请注意,您需要从表单中的字段列表和模板中删除consultation_id,否则它将永远无效。