Opened 15 years ago

Closed 15 years ago

#11925 closed (fixed)

Extension to ModelForm

Reported by: andrew.mcmurry@… Owned by: nobody
Component: Forms Version: dev
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

Description

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)

models.py.diff (2.4 KB ) - added by andrew.mcmurry@… 15 years ago.

Download all attachments as: .zip

Change History (3)

by andrew.mcmurry@…, 15 years ago

Attachment: models.py.diff added

comment:1 by orutherfurd, 15 years ago

Cc: orutherfurd@… added

comment:2 by Russell Keith-Magee, 15 years ago

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