如何将值从mysql传递到创建视图中的表单字段
Posted
技术标签:
【中文标题】如何将值从mysql传递到创建视图中的表单字段【英文标题】:How to Pass a value from mysql to a form field in create view 【发布时间】:2018-08-12 22:30:51 【问题描述】:我有一个 PatientConsultationView,其中有一个 createview 类创建,一个 AllConsultationView 列出所有咨询。我想将咨询 ID 传递给 PatientObservationView 并使用它来填充 PatientObservationForm 或者,只需将其保存在 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(),
模型.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
我的 PatientConsultationAdd.html 模板 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】:您可以使用Models and request.user 下的modelform 文档中描述的技术从request 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
,否则它将永远无效。
【讨论】:
非常感谢。 @daniel Roseman return super() 给出错误:Python 2.7 版不支持这种语法,Super() 应该有一个参数。那么,我如何使它与 python2.7 兼容 更新到 2.7 语法。以上是关于如何将值从mysql传递到创建视图中的表单字段的主要内容,如果未能解决你的问题,请参考以下文章