私はdjango v1.8を使用しています。Django:多くのフォームを1つのテーブルに格納
Aテーブルを4つのフォームに分割します。私views.py
ext_cent = ExternalCentersForm(request.POST, prefix='extcent')
ext_cent_diagnostic = ExternalCentersDiagnosticForm(request.POST,prefix='extcentDiagn')
ext_cent_outcomes = ExternalCentersOutcomesForm(request.POST,prefix='extcentOutcomes')
ext_cent_outcomes2 = ExternalCentersOutcomes2Form(request.POST,prefix='extcentOutcomesTwo')
から
例私はそれらを保存しようとしている私は
ext_cent_object = ext_cent.save(commit=False)
ext_cent_object.author = request.user
ext_cent_object.save()
ext_cent_diagnostic_object = ext_cent_diagnostic.save(commit=False)
ext_cent_diagnostic_object.author = request.user
ext_cent_diagnostic_object.save()
ext_cent_outcomes_object = ext_cent_outcomes.save(commit=False)
ext_cent_outcomes_object.author = request.user
ext_cent_outcomes_object.save()
ext_cent_outcomes2_object = ext_cent_outcomes2.save(commit=False)
ext_cent_outcomes2_object.author = request.user
ext_cent_outcomes2_object.save()
マイforms.py
を使用します。models.py
class ExternalCentersForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ExternalCentersForm, self).__init__(*args, **kwargs)
self.helper=FormHelper(self)
#self.fields['patient'].queryset = Demographic.objects.filter(patient_id=self.instance.patient)
self.helper.field_class = 'col-md-8'
self.helper.label_class = 'col-md-3'
self.helper.layout = Layout(
Fieldset(
'<b>Center Information</b>',
Div(
#HTML(u'<br/><div class="col-md-9"><h4><b>Molecular analysis</b></h4></div><br/><br/>'),
Div('location_of_center',css_class='col-md-6'),
Div('name_of_center',css_class="col-md-6"),
Div('type_of_center',css_class="col-md-6"),
css_class='row',
),
),
FormActions(
Submit('submit', "Save changes"),
Submit('cancel',"Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = Ext_centers
exclude = ['center_id', 'author']
list_display = ('title', 'pub_date', 'author')
class ExternalCentersDiagnosticForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ExternalCentersDiagnosticForm, self).__init__(*args, **kwargs)
self.helper=FormHelper(self)
#self.fields['patient'].queryset = Demographic.objects.filter(patient_id=self.instance.patient)
self.helper.field_class = 'col-md-8'
self.helper.label_class = 'col-md-3'
self.helper.layout = Layout(
Fieldset(
'<b>Diagnostic categories</b>',
Div(
HTML(u'<div class="col-md-9"><h4><b>Paroxysmal nocturnal haemoglobinuria (PNH)</b></h4></div>'),
Div('diagn_categ_pnh_no_patient',css_class='col-md-6'),
Div('diagn_categ_pnh_distribution',css_class="col-md-6"),
css_class='row',
),
),
FormActions(
Submit('submit', "Save changes"),
Submit('cancel',"Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = Ext_centers
exclude = ['center_id', 'author']
list_display = ('title', 'pub_date', 'author')
class ExternalCentersOutcomesForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ExternalCentersOutcomesForm, self).__init__(*args, **kwargs)
self.helper=FormHelper(self)
#self.fields['patient'].queryset = Demographic.objects.filter(patient_id=self.instance.patient)
self.helper.field_class = 'col-md-8'
self.helper.label_class = 'col-md-3'
self.helper.layout = Layout(
Fieldset(
'<b>1. Deaths</b>',
Div(
HTML(u'<div class="col-md-9"><h4><b>2015</b></h4></div>'),
Div('outcomes_year2015_thal',css_class='col-md-6'),
Div('outcomes_year2015_sickle',css_class="col-md-6"),
Div('outcomes_year2015_rare',css_class="col-md-6"),
css_class='row',
),
),
FormActions(
Submit('submit', "Save changes"),
Submit('cancel',"Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = Ext_centers
exclude = ['center_id', 'author']
list_display = ('title', 'pub_date', 'author')
class ExternalCentersOutcomes2Form(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ExternalCentersOutcomes2Form, self).__init__(*args, **kwargs)
self.helper=FormHelper(self)
#self.fields['patient'].queryset = Demographic.objects.filter(patient_id=self.instance.patient)
self.helper.field_class = 'col-md-8'
self.helper.label_class = 'col-md-3'
self.helper.layout = Layout(
Fieldset(
'<b>2. More measurements</b>',
Div(
#HTML(u'<br/><div class="col-md-9"><h4><b>Molecular analysis</b></h4></div><br/><br/>'),
Div('out_patients_married',css_class='col-md-6'),
Div('out_patients_divorced',css_class="col-md-6"),
Div('out_patients_single',css_class="col-md-6"),
Div('out_patients_cohabiting',css_class="col-md-6"),
Div('out_patients_parented_children',css_class="col-md-6"),
Div('out_thal_women_preg',css_class="col-md-6"),
Div('out_patients_splene',css_class="col-md-6"),
css_class='row',
),
),
FormActions(
Submit('submit', "Save changes"),
Submit('cancel',"Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = Ext_centers
exclude = ['center_id', 'author']
list_display = ('title', 'pub_date', 'author')
例
各フォームの結果として、特定のフォームの値のみを含むテーブルに新しい行があります。これらのフォームのすべてのデータを1つのテーブルに格納する必要があります。あなたを保存する代わりに
フォームにもご記入ください。 – itzMEonTV
@itzmeontv私のフォームを今追加しました。 – zinon