﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18064	Allow changing form field properties after form creation	Chris Wilson	nobody	"Currently, if I want to tweak the properties of some fields in a ModelForm, I have to replace them in the subclass like this:

{{{
class DocumentForm(ModelForm):
    title = models.Document._meta.get_field('title').formfield(required=False)
    programs = models.Document._meta.get_field('programs').formfield(required=False)
    authors = models.Document._meta.get_field('authors').formfield(required=False)
    confidential = models.Document._meta.get_field('confidential').formfield(widget=AdminYesNoWidget)
    uploader = models.Document._meta.get_field('uploader').formfield(required=False)
    file = models.Document._meta.get_field('file').formfield(widget=AdminFileWidgetWithSize)
}}}

This is very bulky to just change some properties. The problem is that the field copies some of its properties into weird places like the widget during initialisation:

{{{
class Field(object):
    ...
    def __init__(...)
        ...
        if help_text is None:
            self.help_text = u''
        else:
            self.help_text = smart_unicode(help_text)
        widget = widget or self.widget
        if isinstance(widget, type):
            widget = widget()

        # Trigger the localization machinery if needed.
        self.localize = localize
        if self.localize:
            widget.is_localized = True

        # Let the widget know whether it should display as required.
        widget.is_required = self.required

        # Hook into self.widget_attrs() for any Field-specific HTML attributes.
        extra_attrs = self.widget_attrs(widget)
        if extra_attrs:
            widget.attrs.update(extra_attrs)

        self.widget = widget
}}}

If we refactored this so that all the property initialisation was done in setters, then we could just write:

{{{
class DocumentForm(ModelForm):
    def __init__(...)
        self['title'].required = False
        self['programs'].help_text = 'Hold down Ctrl to select multiple options'
        self['authors'].required = False
        self['confidential'].widget = AdminYesNoWidget
        self['uploader'].required = False
        self['file'].widget = AdminFileWidgetWithSize
}}}

Which is more concise, much clearer, and does not override things unnecessarily.

I can write the code, but first I want to:

* see if any core developers object, to avoid wasting effort
* know how to run the Django tests and which suite(s) I should be running
* know where to put the tests."	New feature	closed	Forms	1.4	Normal	duplicate			Unreviewed	0	0	0	0	0	0
