Opened 15 years ago
Closed 15 years ago
#11925 closed (fixed)
Extension to ModelForm
Reported by: | 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)
Change History (3)
by , 15 years ago
Attachment: | models.py.diff added |
---|
comment:1 by , 15 years ago
Cc: | added |
---|
comment:2 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
This has been fixed by the introduction of the widget argument for ModelForm.