diff --git a/django/newforms/fields.py b/django/newforms/fields.py index bcd30dbc92..e9e1fb7746 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -35,7 +35,7 @@ class Field(object): # Tracks each time a Field instance is created. Used to retain order. creation_counter = 0 - def __init__(self, required=True, widget=None, label=None, initial=None): + def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None): # required -- Boolean that specifies whether the field is required. # True by default. # widget -- A Widget class, or instance of a Widget class, that should be @@ -47,9 +47,11 @@ class Field(object): # field name, if the Field is part of a Form. # initial -- A value to use in this Field's initial display. This value is # *not* used as a fallback if data isn't given. + # help_text -- An optional string to use as "help text" for this Field. if label is not None: label = smart_unicode(label) self.required, self.label, self.initial = required, label, initial + self.help_text = help_text widget = widget or self.widget if isinstance(widget, type): widget = widget() @@ -85,9 +87,9 @@ class Field(object): return {} class CharField(Field): - def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None): + def __init__(self, max_length=None, min_length=None, *args, **kwargs): self.max_length, self.min_length = max_length, min_length - super(CharField, self).__init__(required, widget, label, initial) + super(CharField, self).__init__(*args, **kwargs) def clean(self, value): "Validates max_length and min_length. Returns a Unicode object." @@ -106,9 +108,9 @@ class CharField(Field): return {'maxlength': str(self.max_length)} class IntegerField(Field): - def __init__(self, max_value=None, min_value=None, required=True, widget=None, label=None, initial=None): + def __init__(self, max_value=None, min_value=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value - super(IntegerField, self).__init__(required, widget, label, initial) + super(IntegerField, self).__init__(*args, **kwargs) def clean(self, value): """ @@ -137,8 +139,8 @@ DEFAULT_DATE_INPUT_FORMATS = ( ) class DateField(Field): - def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None): - super(DateField, self).__init__(required, widget, label, initial) + def __init__(self, input_formats=None, *args, **kwargs): + super(DateField, self).__init__(*args, **kwargs) self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS def clean(self, value): @@ -166,8 +168,8 @@ DEFAULT_TIME_INPUT_FORMATS = ( ) class TimeField(Field): - def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None): - super(TimeField, self).__init__(required, widget, label, initial) + def __init__(self, input_formats=None, *args, **kwargs): + super(TimeField, self).__init__(*args, **kwargs) self.input_formats = input_formats or DEFAULT_TIME_INPUT_FORMATS def clean(self, value): @@ -200,8 +202,8 @@ DEFAULT_DATETIME_INPUT_FORMATS = ( ) class DateTimeField(Field): - def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None): - super(DateTimeField, self).__init__(required, widget, label, initial) + def __init__(self, input_formats=None, *args, **kwargs): + super(DateTimeField, self).__init__(*args, **kwargs) self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS def clean(self, value): @@ -224,14 +226,13 @@ class DateTimeField(Field): raise ValidationError(gettext(u'Enter a valid date/time.')) class RegexField(Field): - def __init__(self, regex, max_length=None, min_length=None, error_message=None, - required=True, widget=None, label=None, initial=None): + def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs): """ regex can be either a string or a compiled regular expression object. error_message is an optional error message to use, if 'Enter a valid value' is too generic for you. """ - super(RegexField, self).__init__(required, widget, label, initial) + super(RegexField, self).__init__(*args, **kwargs) if isinstance(regex, basestring): regex = re.compile(regex) self.regex = regex @@ -263,8 +264,9 @@ email_re = re.compile( r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain class EmailField(RegexField): - def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None): - RegexField.__init__(self, email_re, max_length, min_length, gettext(u'Enter a valid e-mail address.'), required, widget, label, initial) + def __init__(self, max_length=None, min_length=None, *args, **kwargs): + RegexField.__init__(self, email_re, max_length, min_length, + gettext(u'Enter a valid e-mail address.'), *args, **kwargs) url_re = re.compile( r'^https?://' # http:// or https:// @@ -280,9 +282,9 @@ except ImportError: URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)' class URLField(RegexField): - def __init__(self, max_length=None, min_length=None, required=True, verify_exists=False, widget=None, label=None, - initial=None, validator_user_agent=URL_VALIDATOR_USER_AGENT): - super(URLField, self).__init__(url_re, max_length, min_length, gettext(u'Enter a valid URL.'), required, widget, label, initial) + def __init__(self, max_length=None, min_length=None, verify_exists=False, + validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs): + super(URLField, self).__init__(url_re, max_length, min_length, gettext(u'Enter a valid URL.'), *args, **kwargs) self.verify_exists = verify_exists self.user_agent = validator_user_agent @@ -328,10 +330,8 @@ class NullBooleanField(BooleanField): return {True: True, False: False}.get(value, None) class ChoiceField(Field): - def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None): - if isinstance(widget, type): - widget = widget() - super(ChoiceField, self).__init__(required, widget, label, initial) + def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None, help_text=None): + super(ChoiceField, self).__init__(required, widget, label, initial, help_text) self.choices = choices def _get_choices(self): @@ -362,8 +362,8 @@ class ChoiceField(Field): class MultipleChoiceField(ChoiceField): hidden_widget = MultipleHiddenInput - def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None): - super(MultipleChoiceField, self).__init__(choices, required, widget, label, initial) + def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None, help_text=None): + super(MultipleChoiceField, self).__init__(choices, required, widget, label, initial, help_text) def clean(self, value): """ @@ -390,8 +390,8 @@ class ComboField(Field): """ A Field whose clean() method calls multiple Field clean() methods. """ - def __init__(self, fields=(), required=True, widget=None, label=None, initial=None): - super(ComboField, self).__init__(required, widget, label, initial) + def __init__(self, fields=(), *args, **kwargs): + super(ComboField, self).__init__(*args, **kwargs) # Set 'required' to False on the individual fields, because the # required validation will be handled by ComboField, not by those # individual fields. @@ -425,8 +425,8 @@ class MultiValueField(Field): You'll probably want to use this with MultiWidget. """ - def __init__(self, fields=(), required=True, widget=None, label=None, initial=None): - super(MultiValueField, self).__init__(required, widget, label, initial) + def __init__(self, fields=(), *args, **kwargs): + super(MultiValueField, self).__init__(*args, **kwargs) # Set 'required' to False on the individual fields, because the # required validation will be handled by MultiValueField, not by those # individual fields. @@ -481,9 +481,9 @@ class MultiValueField(Field): raise NotImplementedError('Subclasses must implement this method.') class SplitDateTimeField(MultiValueField): - def __init__(self, required=True, widget=None, label=None, initial=None): + def __init__(self, *args, **kwargs): fields = (DateField(), TimeField()) - super(SplitDateTimeField, self).__init__(fields, required, widget, label, initial) + super(SplitDateTimeField, self).__init__(fields, *args, **kwargs) def compress(self, data_list): if data_list: diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 1724a5a69f..7e29941bef 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -26,12 +26,15 @@ class SortedDictFromList(SortedDict): self.keyOrder = [d[0] for d in data] dict.__init__(self, dict(data)) + def copy(self): + return SortedDictFromList(self.items()) + class DeclarativeFieldsMetaclass(type): - "Metaclass that converts Field attributes to a dictionary called 'fields'." + "Metaclass that converts Field attributes to a dictionary called 'base_fields'." def __new__(cls, name, bases, attrs): fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) - attrs['fields'] = SortedDictFromList(fields) + attrs['base_fields'] = SortedDictFromList(fields) return type.__new__(cls, name, bases, attrs) class BaseForm(StrAndUnicode): @@ -46,6 +49,12 @@ class BaseForm(StrAndUnicode): self.prefix = prefix self.initial = initial or {} self.__errors = None # Stores the errors after clean() has been called. + # The base_fields class attribute is the *class-wide* definition of + # fields. Because a particular *instance* of the class might want to + # alter self.fields, we create self.fields here by copying base_fields. + # Instances should always modify self.fields; they should not modify + # self.base_fields. + self.fields = self.base_fields.copy() def __unicode__(self): return self.as_table() @@ -85,7 +94,7 @@ class BaseForm(StrAndUnicode): """ return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name - def _html_output(self, normal_row, error_row, row_ender, errors_on_separate_row): + def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row): "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." top_errors = self.non_field_errors() # Errors that should be displayed above all fields. output, hidden_fields = [], [] @@ -100,7 +109,11 @@ class BaseForm(StrAndUnicode): if errors_on_separate_row and bf_errors: output.append(error_row % bf_errors) label = bf.label and bf.label_tag(escape(bf.label + ':')) or '' - output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf)}) + if field.help_text: + help_text = help_text_html % field.help_text + else: + help_text = u'' + output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text}) if top_errors: output.insert(0, error_row % top_errors) if hidden_fields: # Insert any hidden fields in the last row. @@ -115,15 +128,15 @@ class BaseForm(StrAndUnicode): def as_table(self): "Returns this form rendered as HTML
s." - return self._html_output(u'
%(label)s %(field)s
', u'%s
', '', True) + return self._html_output(u'%(label)s %(field)s%(help_text)s
', u'%s
', '', u' %s', True) def non_field_errors(self): """ diff --git a/django/newforms/models.py b/django/newforms/models.py index 819e70d775..9bd6a3ff83 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -15,10 +15,7 @@ def model_save(self, commit=True): """ if self.errors: raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) - obj = self._model(**self.clean_data) - if commit: - obj.save() - return obj + return save_instance(self, self._model(), commit) def save_instance(form, instance, commit=True): """ @@ -33,12 +30,18 @@ def save_instance(form, instance, commit=True): if form.errors: raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) clean_data = form.clean_data - for f in opts.fields + opts.many_to_many: + for f in opts.fields: if isinstance(f, models.AutoField): continue setattr(instance, f.attname, clean_data[f.name]) if commit: instance.save() + for f in opts.many_to_many: + setattr(instance, f.attname, getattr(instance, f.attname).model.objects.filter(pk__in = clean_data[f.name])) + # GOTCHA: If many-to-many data is given and commit=False, the many-to-many + # data will be lost. This happens because a many-to-many options cannot be + # set on an object until after it's saved. Maybe we should raise an + # exception in that case. return instance def make_instance_save(instance): @@ -64,7 +67,7 @@ def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfiel if formfield: field_list.append((f.name, formfield)) fields = SortedDictFromList(field_list) - return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'save': model_save}) + return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': model_save}) def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): """ @@ -87,9 +90,9 @@ def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kw field_list.append((f.name, formfield)) fields = SortedDictFromList(field_list) return type(opts.object_name + 'InstanceForm', (form,), - {'fields': fields, '_model': model, 'save': make_instance_save(instance)}) + {'base_fields': fields, '_model': model, 'save': make_instance_save(instance)}) def form_for_fields(field_list): "Returns a Form class for the given list of Django database field instances." fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list]) - return type('FormForFields', (BaseForm,), {'fields': fields}) + return type('FormForFields', (BaseForm,), {'base_fields': fields}) diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index c71810465e..585e12cc18 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -136,9 +136,11 @@ class CheckboxInput(Widget): class Select(Widget): def __init__(self, attrs=None, choices=()): - # choices can be any iterable self.attrs = attrs or {} - self.choices = choices + # choices can be any iterable, but we may need to render this widget + # multiple times. Thus, collapse it into a list so it can be consumed + # more than once. + self.choices = list(choices) def render(self, name, value, attrs=None, choices=()): if value is None: value = '' @@ -256,11 +258,16 @@ class RadioSelect(Select): class CheckboxSelectMultiple(SelectMultiple): def render(self, name, value, attrs=None, choices=()): if value is None: value = [] + has_id = attrs and attrs.has_key('id') final_attrs = self.build_attrs(attrs, name=name) output = [u'Subject: 100 characters max.
+Message:
+Sender: A valid e-mail address, please.
+Cc myself:
+ Dynamic initial values ---------------------- diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 620565721c..81a7ec2abf 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -224,6 +224,47 @@ Add some categories and test the many-to-many form output. +>>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', +... 'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}) +>>> new_art = f.save() +>>> new_art.id +1 +>>> new_art = Article.objects.get(id=1) +>>> new_art.categories.all() +[Username: e.g., user@example.com
+Password: Choose wisely.
+>>> print p.as_table() +