TLDR 问题:如何使用“分段”(不确定这是否被视为内联)布局和多个模型(有些相关,有些不相关)制作一个脆脆的表单。
我正在尝试理解 Django 中的几件事:表单、表单集、嵌套表单和脆皮,我已经研究了一段时间了,感觉我已经很接近了,只是需要有人帮助连接这些点。我不知道如何在没有脆皮的情况下完成它,所以我开始沿着这条路走,认为脆皮是解决方案。如有错误请指正,谢谢:)
我想要一种表单(如 HTML 表单,不一定是 Django 表单),它有一个包含大量字段的主模型,但在主字段中间有二级/三级模型。我相当接近布局,但似乎无法让二级/三级模型在布局中间渲染,更不用说在没有酥脆/django 错误的情况下进行编译了。
Here is a color-coded visual of what I am trying to attain
我认为我至少有以下一项错误:
- 我没有调用正确的 formfactory
- 我没有正确使用表单集
- 我没有在表单助手的布局中正确地将表单字段引用到正确的模型字段
- 布局是不可能的,或者我应用了错误的代码结构来获得结果。
- 我不认为我可以直接调用两个表单,因为它们不会嵌套/集成
上面列表项的代码(不能直接在下面放置代码块
#I don't think this will achieve the integration/nested look I am aiming for
#views.py:
parent_form = ParentForm()
child_form = ChildForm()
render(template.html, {
"pform": parent_form,
"cform": child_form
})
#template.html:
<form>
{{ pform }}
{{ cform }}
</form>
文件供参考
模型.py
#Black in the picture
class Truck(models.Model):
name = models.CharField(…)
…
#Blue in the picture
class QuickInspection(models.Model):
odometer = models.IntegerField(…)
… (created_at, user_cookie#who did it, …)
truck = models.ForeignKey(Truck)
-----
#These two are unrelated to the Truck in the DB, and I would prefer to keep it that way, if for at least to understand how to accomplish this
-----
#Red
class Tires(models.Model):
front_tire = models.CharField(…)
… (created_at, …)
truck = models.ForeignKey(Truck)
full_inspection = models.ForeignKey(FullInspection, blank=True, null=True) #optional, and if it has this foreign key, then I know the Tires were looked at in a full inspection. If not, then they were looked at in the quick inspection, without having a foreign key to the QuickInspection
#Green
class Brakes(models.Model):
front_axle = models.CharField(…)
…
createdAt = models.DateTimeField(auto_now_add=True)
truck = models.ForeignKey(Truck)
pm = models.ForeignKey(PM, blank=True, null=True)
full_inspection = models.ForeignKey(FullInspection, blank=True, null=True) #optional, same as full_inspection in Tires
views.py
def weeklyView(request, truckID):
# POST
if request.method == 'POST':
# Check forms for valid data and save or provide error
#return response
# GET
else:
#make each form individually?
quickForm = OHReadingForm(…)
tireForm = TireForm()
brakeForm = BrakeForm()
#Or import a formset and helper?
formset = ExampleFormSet()
helper = ExampleFormSetHelper()
response = render(request, 'trucks/weeklyInspection.html', {
'ohrForm': ohrForm,
'formset': formset,
'helper': helper,
'tireForm': tireForm,
'truck': truck,
})
forms.py
class QuickInspectionForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(QuickInspectionForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.form_method = 'post'
self.helper.form_action = 'quickInspectionURL'
self.helper.layout = Layout(
Div(
Div(
Fieldset(
'', # 'first arg is the legend of the fieldset',
'quickInspectionMetric1', #From QuickInspection.metric1
'quickInspectionMetric2', #From QuickInspection.metric2
'quickInspectionMetric3', #From QuickInspection.metric3
),
css_class="blue"
),
Div(
Fieldset(
'tireMetric1', #from Tire.metric1
'tireMetric2', #from Tire.metric2
css_class="red"
),
Div(
Fieldset(
'brakeMetric1', #from Brake.metric1
'brakeMetric2', #from Brake.metric2
css_class="green"
),
Div(
Fieldset(
'quickInspectionMetric4', #from QuickInspection.metric4
'quickInspectionMetric5', #from QuickInspection.metric5
css_class="blue"
),
),
Div(
FormActions(
Reset('reset', 'Reset'),
Submit('submit', 'Submit') #submit for all
)
),
)
class Meta:
model = QuickInspection
fields = [
'metric1','metric2','metric3','metric4','metric5',
'truck',
…,
]
ExampleFormSet = formset_factory(QuickInspectionForm, extra=1)
# Other failed attempts
# ExampleFormSet = inlineformset_factory(QuickInspectionForm, extra=1)
# ExampleFormSet = inlineformset_factory(QuickInspectionForm, TireForm, extra=1)
# ExampleFormSet = inlineformset_factory(QuickInspectionForm, TireForm, BrakeForm, extra=1)
class ExampleFormSetHelper(FormHelper):
def __init__(self, *args, **kwargs):
super(ExampleFormSetHelper, self).__init__(*args, **kwargs)
self.form_method = 'post'
self.form_tag = False
self.layout = Layout(…)
#Same as Brake Form
class TireForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(TCForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.form_action = 'tireURL'
self.helper.layout = Layout(…)
class Meta:
model = TireCondition
fields = [
'metric1', 'metric2', …
'truck',
]
用于代码仓库的 JS 小提琴 https://jsfiddle.net/chrisfrisina/eaow6pzy/。我不知道类似 DJango 的 Fiddle 环境......