수색…


통사론

  • NewFormSet = formset_factory (SomeForm, extra = 2)
  • formset = NewFormSet (초기 = [{ 'some_field': '필드 값', 'other_field': '기타 필드 값',}])

초기화되고 단위화된 데이터가있는 Formset

Formset 은 데이터 그리드처럼 한 페이지에 여러 폼을 렌더링하는 방법입니다. 예 :이 ChoiceForm 은 정렬에 대한 몇 가지 질문과 연관 될 수 있습니다. 아이들은 나이 사이에 가장 지능이 있습니까?

appname/forms.py

from django import forms
class ChoiceForm(forms.Form):
    choice = forms.CharField()
    pub_date = forms.DateField()

당신의보기에서 formset_factory 생성자를 사용합니다. Form 을 매개 변수로 취합니다.이 경우 ChoiceForm 과 초기화 된 폼 / 폼 이외의 추가 폼의 extra 를 설명하는 extra는 어떤 것과 마찬가지로 formset 개체를 반복 할 수 있습니다. 다른 iterable.

formset이 데이터로 초기화되지 않은 경우 extra + 1 과 같은 양식 수가 인쇄되고 formset이 초기화되면 초기화된 양식 이외의 extra 의 빈 양식이있는 경우 initialized + extra 가 인쇄됩니다.

appname/views.py

import datetime
from django.forms import formset_factory
from appname.forms import ChoiceForm
    ChoiceFormSet = formset_factory(ChoiceForm, extra=2)
    formset = ChoiceFormSet(initial=[
      {'choice': 'Between 5-15 ?',
        'pub_date': datetime.date.today(),}
      ])

formset : form (form.as_table ())에서 formset에 대해 이와 같이 formset object 를 반복하면

Output in rendered template

<tr>
<th><label for="id_form-0-choice">Choice:</label></th>
<td><input type="text" name="form-0-choice" value="Between 5-15 ?" id="id_form-0-choice" /></td>
</tr>
<tr>
<th><label for="id_form-0-pub_date">Pub date:</label></th>
<td><input type="text" name="form-0-pub_date" value="2008-05-12" id="id_form-0-pub_date" /></td>
</tr>
<tr>
<th><label for="id_form-1-choice">Choice:</label></th>
<td><input type="text" name="form-1-choice" id="id_form-1-choice" /></td>
</tr>
<tr>
<th><label for="id_form-1-pub_date">Pub date:</label></th>
<td><input type="text" name="form-1-pub_date" id="id_form-1-pub_date" /></td
</tr>
<tr>
<th><label for="id_form-2-choice">Choice:</label></th>
<td><input type="text" name="form-2-choice" id="id_form-2-choice" /></td>
</tr>
<tr>
<th><label for="id_form-2-pub_date">Pub date:</label></th>
<td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td>
</tr>


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow