Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my crispy formset layouts being ignored?

I'm developing a front end form for a fairly complex model and I am still new to crispy forms.

My Proposal model uses two m2m fields for sections and requirements, each with customized through tables. I'm presenting the proposal form using formsets with this templating:

    <div class="tab-content panel-body">
        <div id="basic-information"
             class="tab-pane active" >
            {% crispy form %}
        </div>
        <div id="sections"
             class="tab-pane" >
            {% crispy sectionmap_formset sectionmap_formset_helper %}
        </div>
        <div id="requirements"
             class="tab-pane" >
            {% crispy requirementmap_formset requirementmap_formset_helper %}
        </div>
    </div>

The helpers are set up thus:

class MapFormSetHelper(FormHelper):
    template = 'bootstrap/table_inline_formset.html'

    def __init__(self, form=None):
        super(MapFormSetHelper, self).__init__(form)
        self.disable_csrf = True
        self.form_method = 'post'
        self.form_tag = False


class SectionMapFormSetHelper(MapFormSetHelper):
    def __init__(self, form=None):
        super(SectionMapFormSetHelper, self).__init__(form)
        self.layout = Layout('order', 'section', 'placement')


class RequirementMapFormSetHelper(MapFormSetHelper):
    def __init__(self, form=None):
        super(RequirementMapFormSetHelper, self).__init__(form)
        self.layout = Layout('order', 'requirement', 'selected_option')

and added to the context thus:

def get_context_data(self, **kwargs):
    context = super(ProposalUpdateView, self).get_context_data(**kwargs)
    proposal = context['proposal']

    # Add formsets
    for model_class, context_key, formset_class in (
            (pm.ProposalSectionMap, 'sectionmap_formset', forms.SectionMapFormSet),
            (pm.ProposalRequirementMap, 'requirementmap_formset', forms.RequirementMapFormSet)):
        qs = model_class.objects.filter(proposal=proposal)
        context[context_key] = formset_class(queryset=qs)

    # Add helpers
    context['sectionmap_formset_helper'] = forms.SectionMapFormSetHelper()
    context['requirementmap_formset_helper'] = forms.RequirementMapFormSetHelper()

    return context

The tabular layout and form_tag bits are picked up fine (and the result looks good) but the self.layout settings are having no effect.

The problem is that all of the formset fields are still being displayed - although the lines are invoked and the right helpers are in place, it is as if the layouts are just being ignored (I've tried adding html to the layouts and it is not displayed).

Can anyone tell me what I'm doing wrong? Feel free to point me at the right bit of documentation - I've pored over it to no avail and I am probably missing something obvious.

like image 955
Paul Whipp Avatar asked Oct 31 '22 17:10

Paul Whipp


1 Answers

OK, the answer is in the fact that I've used the bootstrap/table_inline_formset.html. Looking at it in detail, I see that it works directly through the form fields thus ignoring the crispy layout. -1 for crispy in this regard.

To specify my fields I created a customized model form for the formset and added the fields to its Meta thus:

class SectionMapForm(forms.ModelForm):
    class Meta:
        model = pm.ProposalSectionMap
        fields = ('order', 'section', 'placement')

SectionMapFormSet = modelformset_factory(pm.ProposalSectionMap, form=SectionMapForm, extra=1)


class RequirementMapForm(forms.ModelForm):
    class Meta:
        model = pm.ProposalRequirementMap
        fields = ('order', 'requirement', 'selected_option')

RequirementMapFormSet = modelformset_factory(pm.ProposalRequirementMap, form=RequirementMapForm, extra=1)

I hope this helps someone.

like image 137
Paul Whipp Avatar answered Nov 15 '22 10:11

Paul Whipp