Django

Code

Changeset 4306

Show
Ignore:
Timestamp:
01/10/07 18:04:27 (2 years ago)
Author:
adrian
Message:

newforms: Added unit tests and docs explaining that clean_data will only ever contain fields of the form, even if extra fields are passed in data

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/newforms.txt

    r4297 r4306  
    254254    ... 
    255255    AttributeError: 'ContactForm' object has no attribute 'clean_data' 
     256 
     257``clean_data`` will always *only* contain a key for fields defined in the 
     258``Form``, even if you pass extra data when you define the ``Form``. In this 
     259example, we pass a bunch of extra fields to the ``ContactForm`` constructor, 
     260but ``clean_data`` contains only the form's fields:: 
     261 
     262    >>> data = {'subject': 'hello', 
     263    ...         'message': 'Hi there', 
     264    ...         'sender': 'foo@example.com', 
     265    ...         'cc_myself': True, 
     266    ...         'extra_field_1': 'foo', 
     267    ...         'extra_field_2': 'bar', 
     268    ...         'extra_field_3': 'baz'} 
     269    >>> f = ContactForm(data) 
     270    >>> f.is_valid() 
     271    True 
     272    >>> f.clean_data # Doesn't contain extra_field_1, etc. 
     273    {'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'} 
    256274 
    257275Behavior of unbound forms 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4304 r4306  
    16471647>>> print p['birthday'] 
    16481648<input type="text" name="birthday" id="id_birthday" /> 
     1649 
     1650clean_data will always *only* contain a key for fields defined in the 
     1651Form, even if you pass extra data when you define the Form. In this 
     1652example, we pass a bunch of extra fields to the form constructor, 
     1653but clean_data contains only the form's fields. 
     1654>>> data = {'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9', 'extra1': 'hello', 'extra2': 'hello'} 
     1655>>> p = Person(data) 
     1656>>> p.is_valid() 
     1657True 
     1658>>> p.clean_data 
     1659{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 
    16491660 
    16501661"auto_id" tells the Form to add an "id" attribute to each form element.