trouselife
trouselife

Reputation: 969

Create crispy form layout in a loop

I have a form which I am using a value from a GET request in order to display values that can be used in the form. I am trying to use crispy form to display a drop down menu for each value, but I do not know how to define the crispyform layout:

class ReportSample(forms.Form):

    def __init__(self, *args, **kwargs):
        super(ReportSample, self).__init__(*args, **kwargs)

        sr_id = self.data.getlist('id')
        current_sr_id = sr_id[0]

        vasr_obj = VariantAnnotationSampleRun.objects.filter(
                        sample_run_id=SampleRun.objects.get(id=current_sr_id)
                    )

        for o in vasr_obj:

            self.fields[str(o.id)] = forms.ChoiceField(
                    choices=(
                        ('dont_report', '-',),
                        ('report', 'Report',),
                        ('to_confirm', 'To confirm',),
                    )
                )

        self.helper = FormHelper()

        self.helper.layout = Layout(
                Submit('submit', 'Submit', css_class='upload-btn'),
            )
        self.helper.form_method = 'POST'

If I were to render this without crispy form in HTML:

{{form}} 

Then I get a dropdown for each value in my vasr_obj. How can I do this by defining the field (assigned to str(o.id) variable) in my layout:

I have tried variations on this:

        self.helper = FormHelper()


        for o in vasr_obj:

            id_field = forms.ChoiceField(
                    choices=(
                        ('dont_report', '-',),
                        ('report', 'Report',),
                        ('to_confirm', 'To confirm',),
                    )
                )

            self.helper.layout = Layout(
                    Field(id_field, css_class='search-sample'),
                    Submit('submit', 'Submit', css_class='upload-btn'),
                   )

        self.helper.form_method = 'POST'

But it gives the error:

WARNING:root:Could not resolve form field '<django.forms.fields.ChoiceField object at 0x7fb1b32d26d8>'.

Upvotes: 0

Views: 1217

Answers (1)

Ivan
Ivan

Reputation: 2675

Try with generating the list of Field objects and passing it to the Layout:

def __init__(self, *args, **kwargs):
    super(ReportSample, self).__init__(*args, **kwargs)

    sr_id = self.data.getlist('id')
    current_sr_id = sr_id[0]

    vasr_obj = VariantAnnotationSampleRun.objects.filter(
        sample_run_id=SampleRun.objects.get(id=current_sr_id)
    )

    fields = []
    for o in vasr_obj:
        str_id = str(o.id)
        fields.append(Field(str_id, css_class='search-sample'))
        self.fields[str_id] = forms.ChoiceField(
            choices=(
                ('dont_report', '-',),
                ('report', 'Report',),
                ('to_confirm', 'To confirm',),
            )
        )

    self.helper = FormHelper()
    self.helper.layout = Layout(
        *fields,
        Submit('submit', 'Submit', css_class='upload-btn'),
    )

    self.helper.form_method = 'POST'

Upvotes: 2

Related Questions