Django 内联表单集设置
我已经将我的课程设置如下:
class InspectionGroup(models.Model):
group = models.CharField(max_length=50)
class InspectionItem(models.Model):
group = models.ForeignKey(InspectionGroup)
item = models.CharField(max_length=50)
class InspectionQuestion(models.Model):
item = models.ForeignKey(InspectionItem)
question = models.CharField(max_length=200)
question_pass = models.BooleanField()
class InspectionResult(models.Model):
question = models.ForeignKey(InspectionQuestion)
vehicle = models.ForeignKey(Vehicle)
result = models.BooleanField()
submitted_by = models.ForeignKey(User, editable=False)
date_time_submitted = models.DateTimeField(default=datetime.today,
editable=False)
我想做的是让用户选择一个检查组,然后从该组中向他们展示该组下的所有问题,其项目标题用于分组模板。然后他们将回答问题并发布结果。
我的问题是完成这件事。我已经开始编写一些代码,但它似乎不正确。
forms.py
QuestionFormSet = inlineformset_factory(InspectionItem, InspectionQuestion,
form=QuestionForm, extra=0, can_delete=False)
class BaseInspectionFormset(BaseInlineFormSet):
def add_fields(self, form, index):
# allow the super class to create the fields as usual
super(BaseInspectionFormset, self).add_fields(form, index)
# created the nested formset
try:
instance = self.get_queryset()[index]
pk_value = instance.pk
except IndexError:
instance=None
pk_value = hash(form.prefix)
# store the formset in the .nested property
form.nested = [QuestionFormSet (instance = instance,
prefix = 'INSPECTIONQUESTIONS_%s' % pk_value)]
InspectionFormset = inlineformset_factory(InspectionGroup, InspectionItem,
formset=BaseInspectionFormset, extra=0, can_delete=False)
view.py
def vehicle_inspection(request, stock_number, id):
vehicle = get_object_or_404(Vehicle, stock_number=stock_number)
group = get_object_or_404(InspectionGroup, pk=id)
if request.method == 'POST':
formset = InspectionFormset(request.POST, instance=group)
if formset.is_valid():
results = formset.save_all()
return HttpResponseRedirect(reverse('inspectionRecord',
kwargs={'stock_number': stock_number}))
else:
formset = InspectionFormset(instance=group)
return render_to_response('vehicles-inspection-form.html', {
'formset': formset})
有人可以确认这个想法是否可行或者我需要做什么才能拥有我想要的功能?目前我收到此错误:
[u'ManagementForm data is missing or has been tampered with']
更新 1 我已通过此链接成功解决了上述错误 来自 readthedocs
但是,目前,表单集似乎是空的,因为模板仅显示提交按钮。
<form action="." method="POST" id="inspection_form">
{% csrf_token %}
<div id="form">
{{ formset.management_form }}
{% for form in formset.forms %}
{{ form }}
{% if form.nested %}
{% for formset in form.nested %}
{{ formset.as_table }}
{% endfor %}
{% endif %}
{% endfor %}
</div>
<div class="form_buttons">
<button type="submit" name="confirm" id="confirm">
<span>Submit</span>
</button>
</div>
</form>
现在可能出现什么问题?
I've setup my classes as below:
class InspectionGroup(models.Model):
group = models.CharField(max_length=50)
class InspectionItem(models.Model):
group = models.ForeignKey(InspectionGroup)
item = models.CharField(max_length=50)
class InspectionQuestion(models.Model):
item = models.ForeignKey(InspectionItem)
question = models.CharField(max_length=200)
question_pass = models.BooleanField()
class InspectionResult(models.Model):
question = models.ForeignKey(InspectionQuestion)
vehicle = models.ForeignKey(Vehicle)
result = models.BooleanField()
submitted_by = models.ForeignKey(User, editable=False)
date_time_submitted = models.DateTimeField(default=datetime.today,
editable=False)
What I want to do is to have the user choose an inspection group, then from that group they'll be presented with all the questions under that group, with their item headers being used for grouping in the template. They'll then answer the questions and post the results.
My problem is getting this done. I had started on some code, but it just doesn't seem right.
forms.py
QuestionFormSet = inlineformset_factory(InspectionItem, InspectionQuestion,
form=QuestionForm, extra=0, can_delete=False)
class BaseInspectionFormset(BaseInlineFormSet):
def add_fields(self, form, index):
# allow the super class to create the fields as usual
super(BaseInspectionFormset, self).add_fields(form, index)
# created the nested formset
try:
instance = self.get_queryset()[index]
pk_value = instance.pk
except IndexError:
instance=None
pk_value = hash(form.prefix)
# store the formset in the .nested property
form.nested = [QuestionFormSet (instance = instance,
prefix = 'INSPECTIONQUESTIONS_%s' % pk_value)]
InspectionFormset = inlineformset_factory(InspectionGroup, InspectionItem,
formset=BaseInspectionFormset, extra=0, can_delete=False)
view.py
def vehicle_inspection(request, stock_number, id):
vehicle = get_object_or_404(Vehicle, stock_number=stock_number)
group = get_object_or_404(InspectionGroup, pk=id)
if request.method == 'POST':
formset = InspectionFormset(request.POST, instance=group)
if formset.is_valid():
results = formset.save_all()
return HttpResponseRedirect(reverse('inspectionRecord',
kwargs={'stock_number': stock_number}))
else:
formset = InspectionFormset(instance=group)
return render_to_response('vehicles-inspection-form.html', {
'formset': formset})
Could someone confirm if this idea will work or what I'll need to do to have the functionality I want? Currently I'm getting this error:
[u'ManagementForm data is missing or has been tampered with']
Update 1
I've managed to resolve the above error by following this link from readthedocs
However, now currently, it seems the formset is empty since the template shows just the submit button.
<form action="." method="POST" id="inspection_form">
{% csrf_token %}
<div id="form">
{{ formset.management_form }}
{% for form in formset.forms %}
{{ form }}
{% if form.nested %}
{% for formset in form.nested %}
{{ formset.as_table }}
{% endfor %}
{% endif %}
{% endfor %}
</div>
<div class="form_buttons">
<button type="submit" name="confirm" id="confirm">
<span>Submit</span>
</button>
</div>
</form>
What can be the issue now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该文档似乎有答案: https:// docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform
编辑:
for formset
而不是formset.forms
?和
{{ form.as_table }}
而不是{{ form }}
。the documentation seems to have the answer: https://docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform
EDIT:
for form in formset
instead offormset.forms
?and
{{ form.as_table }}
instead of{{ form }}
.