Opened 9 years ago

Closed 9 years ago

#11925 closed (fixed)

Extension to ModelForm

Reported by: andrew.mcmurry@… Owned by: nobody
Component: Forms Version: master
Severity: Keywords: model forms
Cc: orutherfurd@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Currently when you want to alter the way a model field is represented as a form field, there is an all or nothing approach. To change any of the form field parameters you have to specify them all, duplicating information from the model. Here is an extension to ModelForm that allows selective parameter overriding. An example of use:

class MyModel (models.Model):
    a = models.CharField(max_length=40)

class MyForm (forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ('a',)
        params = { 'a': { 'widget': forms.TextInput(attrs={'size':40}),
                                     'help_text': "This form field has the right size" } }

Seeing that the default method to generate a form field from a model field accepts a form_class keyword argument, you can use this to alter the field class used, without altering any of the parameters passed to initialise it.

Attachments (1) (2.4 KB) - added by andrew.mcmurry@… 9 years ago.

Download all attachments as: .zip

Change History (3)

Changed 9 years ago by andrew.mcmurry@…

Attachment: added

comment:1 Changed 9 years ago by orutherfurd

Cc: orutherfurd@… added

comment:2 Changed 9 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

This has been fixed by the introduction of the widget argument for ModelForm.

Note: See TracTickets for help on using tickets.
Back to Top