Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing field type in a Django ModelFormset

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?

like image 866
Matt Hampel Avatar asked Jun 28 '09 22:06

Matt Hampel


People also ask

How do you make a field non editable in Django?

Every field comes in with built-in validations from Django validators. One can also add more built-in field validations for applying or removing certain constraints on a particular field. editable=False will make the field disappear from all forms including admin and ModelForm i.e., it can not be edited using any form.

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What does field type signifies in Django models?

Fields in Django are the data types to store a particular type of data. For example, to store an integer, IntegerField would be used. These fields have in-built validation for a particular data type, that is you can not store “abc” in an IntegerField. Similarly, for other fields.


2 Answers

EntryFormSet = modelformset_factory(Entry, form=EntryForm)

like image 96
Harold Avatar answered Sep 28 '22 16:09

Harold


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

like image 23
James Bennett Avatar answered Sep 28 '22 16:09

James Bennett