Changing field type in a Django ModelFormset
Asked Answered
A

2

5

In a Django ModelForm, you can change the widget type of a field like so:

class EntryForm(ModelForm):
    entity = forms.CharField()

    class Meta:
        model = Entry

I can easily create a modelformset from the same model like so:

EntryFormSet = modelformset_factory(Entry)

But is there a way to include the input field type change change when creating a modelformset?

Airel answered 28/6, 2009 at 22:6 Comment(0)
D
13

EntryFormSet = modelformset_factory(Entry, form=EntryForm)

Droit answered 29/6, 2009 at 5:36 Comment(3)
After setting it up as recommended above, passing entry_formset = EntryFormSet(prefix='entries') to render_to_response still displays all the fields. I'm still playing around with this.Airel
docs.djangoproject.com/en/dev/topics/forms/modelforms/…Droit
Ah -- thanks. I expected it to inherit those from my EntryForm model, which also excluded some fields (which I left out from the example above for simplicity)Airel
C
4

modelformset_factory takes a keyword argument form, which -- I believe -- will let you pass your form class and have it used...

Chiron answered 29/6, 2009 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.