2010-04-12 83 views
5

表單將吐出未知數量的待回答問題。每個問題都包含提示,值字段和單位字段。該表單是在運行時在formclass的init方法中構建的。django - 動態表單字段集

編輯:每個問題都會收到一個獨特提示作爲標籤,以及select元素的唯一單位列表。

對於可迭代的表單字段集來說,這似乎是一個完美的選擇,它可以很容易地設計。但由於字段集 - 例如django-form-utils中的字段集被定義爲元組,所以它們是不可變的......我找不到在運行時定義它們的方法。這是可能的,或者可能是另一種解決方案?

編輯:

表單集與initial_data不是答案 - initial_data只是使默認值設置爲一個formset表單字段。項目列表不能通過initial_data發送到choicefield構造函數。

......除非我錯了。

+0

那豈不是更好地使用[表單集(HTTP://docs.djangoproject .com/en/dev/topics/forms/formsets /)而不是字段集?一個問題的自定義Form類(帶有'prompt'屬性),然後使用['initial'關鍵字參數]加載問題數據(http://docs.djangoproject.com/en/dev/topics/forms/formssets /#使用-初始數據上帶有一個-表單集)? – 2010-04-12 21:34:14

+0

表單集不會訣竅。至少不是給定的formset_factory。我必須能夠爲formset中的每個表單的實際字段構造函數提供一些參數 - 值字段的標籤/提示以及choicefield的單元列表。 – 2010-04-20 04:49:02

回答

2

結賬formsets。您應該能夠將N個問題中的每一個的數據傳遞爲initial data。沿着這些線:

question_data = [] 
for question in your_question_list: 
    question_data.append({'prompt': question.prompt, 
          'value': question.value, 
          'units': question.units}) 
QuestionFormSet = formset_factory(QuestionForm, extra=2) 
formset = QuestionFormSet(initial=question_data) 
+0

初始數據用於爲表單元素提供默認值,而不是爲formset中的表單構造提供數據。 – 2010-04-20 04:25:59

0

我用下面的技巧來創建一個動態的formset。從視圖中調用create_dynamic_formset()函數。

def create_dynamic_formset(name_filter): 

    """ 
    -Need to create the classess dynamically since there is no other way to filter 
    """ 
    class FormWithFilteredField(forms.ModelForm): 
     type = forms.ModelChoiceField(queryset=SomeType.objects.filter(name__icontains=name_filter)) 

     class Meta: 
      model=SomeModelClass 

    return modelformset_factory(SomeModelClass, form=FormWithFilteredField) 
1

老問題,但我遇到了類似的問題。到目前爲止,我發現的最接近的是這個片段,它基於幾年前Malcom做過的一篇文章。 http://djangosnippets.org/snippets/1955/

原始代碼段沒有解決模板方面的問題,並將它們拆分爲字段集,但將每個表單添加到其自己的字段集應該可以實現這一點。

forms.py

from django.forms.formsets import Form, BaseFormSet, formset_factory, \ 
      ValidationError 


    class QuestionForm(Form): 
     """Form for a single question on a quiz""" 
     def __init__(self, *args, **kwargs): 
      # CODE TRICK #1 
      # pass in a question from the formset 
      # use the question to build the form 
      # pop removes from dict, so we don't pass to the parent 
      self.question = kwargs.pop('question') 
      super(QuestionForm, self).__init__(*args, **kwargs) 

      # CODE TRICK #2 
      # add a non-declared field to fields 
      # use an order_by clause if you care about order 
      self.answers = self.question.answer_set.all(
        ).order_by('id') 
      self.fields['answers'] = forms.ModelChoiceField(
        queryset=self.answers()) 


    class BaseQuizFormSet(BaseFormSet): 
     def __init__(self, *args, **kwargs): 
      # CODE TRICK #3 - same as #1: 
      # pass in a valid quiz object from the view 
      # pop removes arg, so we don't pass to the parent 
      self.quiz = kwargs.pop('quiz') 

      # CODE TRICK #4 
      # set length of extras based on query 
      # each question will fill one 'extra' slot 
      # use an order_by clause if you care about order 
      self.questions = self.quiz.question_set.all().order_by('id') 
      self.extra = len(self.questions) 
      if not self.extra: 
       raise Http404('Badly configured quiz has no questions.') 

      # call the parent constructor to finish __init__    
      super(BaseQuizFormSet, self).__init__(*args, **kwargs) 

     def _construct_form(self, index, **kwargs): 
      # CODE TRICK #5 
      # know that _construct_form is where forms get added 
      # we can take advantage of this fact to add our forms 
      # add custom kwargs, using the index to retrieve a question 
      # kwargs will be passed to our form class 
      kwargs['question'] = self.questions[index] 
      return super(BaseQuizFormSet, self)._construct_form(index, **kwargs) 


    QuizFormSet = formset_factory(
     QuestionForm, formset=BaseQuizDynamicFormSet) 

views.py

from django.http import Http404 


    def quiz_form(request, quiz_id): 
     try: 
      quiz = Quiz.objects.get(pk=quiz_id) 
     except Quiz.DoesNotExist: 
      return Http404('Invalid quiz id.') 
     if request.method == 'POST': 
      formset = QuizFormSet(quiz=quiz, data=request.POST) 
      answers = [] 
      if formset.is_valid(): 
       for form in formset.forms: 
        answers.append(str(int(form.is_correct()))) 
       return HttpResponseRedirect('%s?a=%s' 
         % (reverse('result-display',args=[quiz_id]), ''.join(answers))) 
     else: 
      formset = QuizFormSet(quiz=quiz) 

     return render_to_response('quiz.html', locals()) 

模板

{% for form in formset.forms %} 
<fieldset>{{ form }}</fieldset> 
{% endfor %} 
0

以下是我用於類似的情況(變量集字段集,每一個包含一組可變字段)。

我用type()函數來建立我的Form類,BetterBaseForm類從django-form-utils

def makeFurnitureForm(): 
    """makeFurnitureForm() function will generate a form with 
    QuantityFurnitureFields.""" 

    furnitures = Furniture.objects.all() 
    fieldsets = {} 
    fields = {} 

    for obj in furnitures: 
     # I used a custom Form Field, but you can use whatever you want. 
     field = QuantityFurnitureField(name = obj.name) 

     fields[obj.name] = field 
     if not obj.room in fieldsets.keys(): 
      fieldsets[obj.room] = [field,] 
     else: 
      fieldsets[obj.room].append(field) 

    # Here I use a double list comprehension to define my fieldsets 
    # and the fields within. 
    # First item of each tuple is the fieldset name. 
    # Second item of each tuple is a dictionnary containing : 
    # -The names of the fields. (I used a list comprehension for this) 
    # -The legend of the fieldset. 
    # You also can add other meta attributes, like "description" or "classes", 
    # see the documentation for further informations. 
    # I added an example of output to show what the dic variable 
    # I create may look like. 
    dic = [(name, {"fields": [field.name for field in fieldsets[name]], "legend" : name}) 
      for name in fieldsets.keys()] 
    print(dic) 
    # Here I return a class object that is my form class. 
    # It inherits from both forms.BaseForm and forms_utils.forms.BetterBaseForm. 
    return (type("FurnitureForm", 
       (forms.BaseForm, form_utils.forms.BetterBaseForm,), 
       {"_fieldsets" : dic, "base_fields" : fields, 
        "_fieldset_collection" : None, '_row_attrs' : {}})) 

下面是如何dic可能看起來像一個例子:

[('fieldset name 1', 
    {'legend': 'fieldset legend 2', 
    'fields' ['field name 1-1']}), 
('fieldset name 2', 
    {'legend': 'fieldset legend 2', 
    'fields' : ['field 1-1', 'field 1-2']})] 

我用BetterBaseForm而非BetterForm出於同樣的原因this article建議使用BaseForm而非Form

這篇文章很有意思,即使它很老,也解釋瞭如何做動態表單(帶有可變域設置)。它還提供了其他方法來實現動態表單。

雖然它沒有解釋如何使用字段集,但它激勵我找到如何去做,並且原理保持不變。

在視圖中使用它很簡單:

return (render(request,'main/form-template.html', {"form" : (makeFurnitureForm())()})) 

,並在模板:

<form method="POST" name="myform" action="."> 
     {% csrf_token %} 
     <div> 
     {% for fieldset in form.fieldsets %} 
     <fieldset> 
      <legend>{{ fieldset.legend }}</legend> 
      {% for field in fieldset %} 
      <div> 
      {% include "main/furniturefieldtemplate.html" with field=field %} 
      </div> 
      {% endfor %} 
     </fieldset> 
     {% endfor %} 
     </div> 
     <input type="submit" value="Submit"/> 
    </form>