Opened 6 years ago

Closed 6 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: UI/UX:


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@… 6 years ago.

Download all attachments as: .zip

Change History (3)

Changed 6 years ago by andrew.mcmurry@…

comment:1 Changed 6 years ago by orutherfurd

  • Cc orutherfurd@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

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

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