0

そのため、1フィールドから100以上の任意のサイズを持つことができる動的フォームがあり、その動的フォームにDjangoのフォームウィザードをどのように使用できるか疑問に思っていました. フィールドは、フィールド ディクショナリに追加することにより、フォームの __init__ で生成されます。

class MyDynamicForm(forms.Form):
def __init__(self, *args, **kwargs):
    parent = kwargs.pop('parent')
    super(MyDynamicForm, self).__init__(*args, *kwargs)
    # Add each element into self.fields
    for element in parent.elements:
        if element.type == TEXT_FIELD:
           self.fields[element.name] = forms.CharField(widget=forms.Textarea)
        elif element.type == CHECKBOX_FIELD:
            self.fields[element.name] = forms.BooleanField()
        elif element.type == SINGLE_CHOICE_FIELD:
            self.fields[element.name] = forms.ChoiceField(choices=element.choices.split(','))
        elif element.type = MULTIPLE_CHOICE_FIELD:
            self.fields[element.name] = forms.MultipleChoiceField(choices=element.choices.split(','))

このフォームクラスを関数でラップして、for element in parent.elements[start:end]各ウィザードクラスを作成するために行っていることではなく、フィールドの一部のみを作成するフォームクラスを返すだけでよいと思いますが、これはそうではないように感じます正しいアプローチ。どうすればいいのですか、正しい方法はありますか?それとも可能ですか?ありがとう!

4

0 に答える 0