summaryrefslogtreecommitdiff
path: root/django/newforms
diff options
context:
space:
mode:
authorBoulder Sprinters <boulder-sprinters@djangoproject.com>2007-04-02 15:36:31 +0000
committerBoulder Sprinters <boulder-sprinters@djangoproject.com>2007-04-02 15:36:31 +0000
commita3053273c8a5d450a0cd73ee8deebc277d8c4170 (patch)
tree040ebe76a8c7814e888c6669deec86d6c7ef8940 /django/newforms
parentb86d69f52920adf8e065bf6952ab6b3814211d4e (diff)
boulder-oracle-sprint: Merged to [4905].
git-svn-id: http://code.djangoproject.com/svn/django/branches/boulder-oracle-sprint@4906 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms')
-rw-r--r--django/newforms/fields.py19
-rw-r--r--django/newforms/models.py5
-rw-r--r--django/newforms/util.py11
-rw-r--r--django/newforms/widgets.py5
4 files changed, 28 insertions, 12 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py
index 8e3da03470..72c5047030 100644
--- a/django/newforms/fields.py
+++ b/django/newforms/fields.py
@@ -38,15 +38,16 @@ class Field(object):
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
- # used for this Field when displaying it. Each Field has a default
- # Widget that it'll use if you don't specify this. In most cases,
- # the default widget is TextInput.
- # label -- A verbose name for this field, for use in displaying this field in
- # a form. By default, Django will use a "pretty" version of the form
- # 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.
+ # widget -- A Widget class, or instance of a Widget class, that should
+ # be used for this Field when displaying it. Each Field has a
+ # default Widget that it'll use if you don't specify this. In
+ # most cases, the default widget is TextInput.
+ # label -- A verbose name for this field, for use in displaying this
+ # field in a form. By default, Django will use a "pretty"
+ # version of the form 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)
diff --git a/django/newforms/models.py b/django/newforms/models.py
index 616c7141e7..a60002b705 100644
--- a/django/newforms/models.py
+++ b/django/newforms/models.py
@@ -36,13 +36,14 @@ def save_instance(form, instance, commit=True):
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:
- if not f.editable or isinstance(f, models.AutoField):
+ if not f.editable or isinstance(f, models.AutoField) or not f.name in clean_data:
continue
setattr(instance, f.name, clean_data[f.name])
if commit:
instance.save()
for f in opts.many_to_many:
- setattr(instance, f.attname, clean_data[f.name])
+ if f.name in clean_data:
+ setattr(instance, f.attname, 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
diff --git a/django/newforms/util.py b/django/newforms/util.py
index 51a8efdde2..f98027c2e3 100644
--- a/django/newforms/util.py
+++ b/django/newforms/util.py
@@ -1,11 +1,20 @@
from django.conf import settings
from django.utils.html import escape
+from django.utils.functional import Promise, lazy
# Converts a dictionary to a single string with key="value", XML-style with
# a leading space. Assumes keys do not need to be XML-escaped.
flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
def smart_unicode(s):
+ if isinstance(s, Promise):
+ # The input is something from gettext_lazy or similar. We don't want to
+ # translate it until render time, so defer the conversion.
+ return smart_unicode_lazy(s)
+ else:
+ return smart_unicode_immediate(s)
+
+def smart_unicode_immediate(s):
if not isinstance(s, basestring):
if hasattr(s, '__unicode__'):
s = unicode(s)
@@ -15,6 +24,8 @@ def smart_unicode(s):
s = unicode(s, settings.DEFAULT_CHARSET)
return s
+smart_unicode_lazy = lazy(smart_unicode_immediate, unicode)
+
class StrAndUnicode(object):
"""
A class whose __str__ returns its __unicode__ as a bytestring
diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py
index 18bba31897..58d8ab1322 100644
--- a/django/newforms/widgets.py
+++ b/django/newforms/widgets.py
@@ -24,7 +24,10 @@ class Widget(object):
is_hidden = False # Determines whether this corresponds to an <input type="hidden">.
def __init__(self, attrs=None):
- self.attrs = attrs or {}
+ if attrs is not None:
+ self.attrs = attrs.copy()
+ else:
+ self.attrs = {}
def render(self, name, value, attrs=None):
"""