Ticket #10427: django-forms-value.2.diff

File django-forms-value.2.diff, 2.9 KB (added by Ludvig Ericson, 15 years ago)

Use empty string instead of None

  • django/forms/forms.py

     
    366366        auto_id = self.auto_id
    367367        if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
    368368            attrs['id'] = auto_id
    369         if not self.form.is_bound:
    370             data = self.form.initial.get(self.name, self.field.initial)
    371             if callable(data):
    372                 data = data()
    373         else:
    374             data = self.data
     369        # Widgets expect unset data to be None, not empty str.
     370        data = self.value
    375371        if not only_initial:
    376372            name = self.html_name
    377373        else:
     
    401397        return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
    402398    data = property(_data)
    403399
     400    def _value(self):
     401        """
     402        Returns the value for this BoundField, as rendered in widgets.
     403        """
     404        if not self.form.is_bound:
     405            val = self.form.initial.get(self.name, self.field.initial)
     406            if callable(val):
     407                val = val()
     408        else:
     409            val = self.data
     410            if val is None:
     411                val = ''
     412        return val
     413    value = property(_value)
     414
    404415    def label_tag(self, contents=None, attrs=None):
    405416        """
    406417        Wraps the given contents in a <label>, if the field has an ID attribute.
  • tests/regressiontests/forms/forms.py

     
    11961196<li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li>
    11971197<li>Password: <input type="password" name="password" /></li>
    11981198
     1199# Bound field values ##########################################################
     1200
     1201It's possible to get to the value which would be used for rendering the widget
     1202for a field by using the BoundField's value attribute.
     1203
     1204>>> class UserRegistration(Form):
     1205...    username = CharField(max_length=10, initial='djangonaut')
     1206...    password = CharField(widget=PasswordInput)
     1207>>> p = UserRegistration({'password': 'foo'})
     1208>>> print 'username.value =', p['username'].value
     1209username.value =
     1210>>> print 'username.data = ', p['username'].data
     1211username.data = None
     1212>>> print 'password.value =', p['password'].value
     1213password.value = foo
     1214>>> print 'password.data =', p['password'].data
     1215password.data = foo
     1216
     1217The value of username is empty because the form is bound -- the value wasn't
     1218specified, and so is empty. This differs if the form were to be unbound:
     1219
     1220>>> p = UserRegistration()
     1221>>> print p['username'].value
     1222djangonaut
     1223
    11991224# Help text ###################################################################
    12001225
    12011226You can specify descriptive text for a field by using the 'help_text' argument
Back to Top