我试图在一个只有2个字段的模型中创建30个字段,我知道它可以使用formset
完成,但在提交表单时不支持save()
方法,所以我将其更改为:modelformset
支持save()
方法,但使用modelformset
会在design.html
中抛出一个错误:ModelFormOptions' object has no attribute 'private_fields'
如何解决这个问题?
from .forms import ColumnForm
from django.forms import modelformset_factory
def design(request):
ColumnFormSet = modelformset_factory(ColumnForm, fields = ('name', 'selec_type'), extra=30)
formset = ColumnFormSet
if request.method == 'POST':
formset = ColumnFormSet(request.POST)
if formset.is_valid():
formset.save()
return redirect('Home')
else:
formset = ColumnFormSet()
return render (request, 'design.html', {'formset':formset})
forms.py:
from django import forms
from .models import Column
class ColumnForm(forms.ModelForm):
class Meta:
model = Column
fields = ['name', 'selec_type']
模板:
<form method='post'>
{% csrf_token %}
{{ formset.management_form}}
{% for form in formset %}
{{form.as_p}}
{% endfor %}
<button type='submit'>Save<button/>
<form/>
Models.py:
class Type(models.Model):
TYPE_OF_DATA = (
('Number', 'Number'),
('character', 'character'),
('check_box', 'check_box'),
('date', 'date'),
('image', 'image'),
)
data_type = models.CharField(max_length=1000, choices= TYPE_OF_DATA)
class Column(models.Model):
name = models.CharField(max_length=100)
selec_type = models.ForeignKey(Type, on_delete=CASCADE)
1条答案
按热度按时间zu0ti5jz1#
Modelfromset不支持我的
forms.py
文件中的表单类:所以,我改变了我的观点:
对此:
x一个一个一个一个x一个一个二个x