Django

Code

Changeset 5237

Show
Ignore:
Timestamp:
05/14/07 11:24:51 (1 year ago)
Author:
mtredinnick
Message:

Changed the fix from [5231] so that the backwards-incompatibility is made more
obvious and everything still has nice names.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/formtools/preview.py

    r4265 r5237  
    2525Subclass FormPreview and define a done() method: 
    2626 
    27     def done(self, request, clean_data): 
     27    def done(self, request, cleaned_data): 
    2828        # ... 
    2929 
     
    114114            if self.security_hash(request, f) != request.POST.get(self.unused_name('hash')): 
    115115                return self.failed_hash(request) # Security hash failed. 
    116             return self.done(request, f.clean_data) 
     116            return self.done(request, f.cleaned_data) 
    117117        else: 
    118118            return render_to_response(self.form_template, 
     
    161161    # METHODS SUBCLASSES MUST OVERRIDE ######################################## 
    162162 
    163     def done(self, request, clean_data): 
    164         "Does something with the clean_data and returns an HttpResponseRedirect." 
     163    def done(self, request, cleaned_data): 
     164        """ 
     165        Does something with the cleaned_data and returns an 
     166        HttpResponseRedirect. 
     167        """ 
    165168        raise NotImplementedError('You must define a done() method on your %s subclass.' % self.__class__.__name__) 
  • django/trunk/django/newforms/forms.py

    r5231 r5237  
    170170    def full_clean(self): 
    171171        """ 
    172         Cleans all of self.data and populates self.__errors and self.clean_data. 
     172        Cleans all of self.data and populates self.__errors and self.cleaned_data. 
    173173        """ 
    174174        errors = ErrorDict() 
     
    176176            self.__errors = errors 
    177177            return 
    178         self.clean_data = {} 
     178        self.cleaned_data = {} 
    179179        for name, field in self.fields.items(): 
    180180            # value_from_datadict() gets the data from the dictionary. 
     
    184184            try: 
    185185                value = field.clean(value) 
    186                 self.clean_data[name] = value 
    187                 if hasattr(self, 'do_clean_%s' % name): 
    188                     value = getattr(self, 'do_clean_%s' % name)() 
    189                 self.clean_data[name] = value 
     186                self.cleaned_data[name] = value 
     187                if hasattr(self, 'clean_%s' % name): 
     188                    value = getattr(self, 'clean_%s' % name)() 
     189                self.cleaned_data[name] = value 
    190190            except ValidationError, e: 
    191191                errors[name] = e.messages 
    192192        try: 
    193             self.clean_data = self.clean() 
     193            self.cleaned_data = self.clean() 
    194194        except ValidationError, e: 
    195195            errors[NON_FIELD_ERRORS] = e.messages 
    196196        if errors: 
    197             delattr(self, 'clean_data') 
     197            delattr(self, 'cleaned_data') 
    198198        self.__errors = errors 
    199199 
     
    205205        association with the field named '__all__'. 
    206206        """ 
    207         return self.clean_data 
     207        return self.cleaned_data 
    208208 
    209209class Form(BaseForm): 
  • django/trunk/django/newforms/models.py

    r5202 r5237  
    1515def save_instance(form, instance, fields=None, fail_message='saved', commit=True): 
    1616    """ 
    17     Saves bound Form ``form``'s clean_data into model instance ``instance``. 
     17    Saves bound Form ``form``'s cleaned_data into model instance ``instance``. 
    1818 
    1919    Assumes ``form`` has a field for every non-AutoField database field in 
     
    2525    if form.errors: 
    2626        raise ValueError("The %s could not be %s because the data didn't validate." % (opts.object_name, fail_message)) 
    27     clean_data = form.clean_data 
     27    cleaned_data = form.cleaned_data 
    2828    for f in opts.fields: 
    29         if not f.editable or isinstance(f, models.AutoField) or not f.name in clean_data: 
     29        if not f.editable or isinstance(f, models.AutoField) or not f.name in cleaned_data: 
    3030            continue 
    3131        if fields and f.name not in fields: 
    3232            continue 
    33         setattr(instance, f.name, clean_data[f.name]) 
     33        setattr(instance, f.name, cleaned_data[f.name]) 
    3434    if commit: 
    3535        instance.save() 
     
    3737            if fields and f.name not in fields: 
    3838                continue 
    39             if f.name in clean_data: 
    40                 setattr(instance, f.attname, clean_data[f.name]) 
     39            if f.name in cleaned_data: 
     40                setattr(instance, f.attname, cleaned_data[f.name]) 
    4141    # GOTCHA: If many-to-many data is given and commit=False, the many-to-many 
    4242    # data will be lost. This happens because a many-to-many options cannot be 
  • django/trunk/docs/newforms.txt

    r5222 r5237  
    231231 
    232232Once you've created a ``Form`` instance with a set of data and validated it, 
    233 you can access the clean data via the ``clean_data`` attribute of the ``Form`` 
     233you can access the clean data via the ``cleaned_data`` attribute of the ``Form`` 
    234234object:: 
    235235 
     
    241241    >>> f.is_valid() 
    242242    True 
    243     >>> f.clean_data 
     243    >>> f.cleaned_data 
    244244    {'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'} 
    245245 
     
    249249 
    250250If your data does *not* validate, your ``Form`` instance will not have a 
    251 ``clean_data`` attribute:: 
     251``cleaned_data`` attribute:: 
    252252 
    253253    >>> data = {'subject': '', 
     
    258258    >>> f.is_valid() 
    259259    False 
    260     >>> f.clean_data 
     260    >>> f.cleaned_data 
    261261    Traceback (most recent call last): 
    262262    ... 
    263     AttributeError: 'ContactForm' object has no attribute 'clean_data' 
    264  
    265 ``clean_data`` will always *only* contain a key for fields defined in the 
     263    AttributeError: 'ContactForm' object has no attribute 'cleaned_data' 
     264 
     265``cleaned_data`` will always *only* contain a key for fields defined in the 
    266266``Form``, even if you pass extra data when you define the ``Form``. In this 
    267267example, we pass a bunch of extra fields to the ``ContactForm`` constructor, 
    268 but ``clean_data`` contains only the form's fields:: 
     268but ``cleaned_data`` contains only the form's fields:: 
    269269 
    270270    >>> data = {'subject': 'hello', 
     
    278278    >>> f.is_valid() 
    279279    True 
    280     >>> f.clean_data # Doesn't contain extra_field_1, etc. 
     280    >>> f.cleaned_data # Doesn't contain extra_field_1, etc. 
    281281    {'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'} 
    282282 
    283 ``clean_data`` will include a key and value for *all* fields defined in the 
     283``cleaned_data`` will include a key and value for *all* fields defined in the 
    284284``Form``, even if the data didn't include a value for fields that are not 
    285285required. In this example, the data dictionary doesn't include a value for the 
    286 ``nick_name`` field, but ``clean_data`` includes it, with an empty value:: 
     286``nick_name`` field, but ``cleaned_data`` includes it, with an empty value:: 
    287287 
    288288    >>> class OptionalPersonForm(Form): 
     
    294294    >>> f.is_valid() 
    295295    True 
    296     >>> f.clean_data 
     296    >>> f.cleaned_data 
    297297    {'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 
    298298 
    299 In this above example, the ``clean_data`` value for ``nick_name`` is set to an 
     299In this above example, the ``cleaned_data`` value for ``nick_name`` is set to an 
    300300empty string, because ``nick_name`` is ``CharField``, and ``CharField``\s treat 
    301301empty values as an empty string. Each field type knows what its "blank" value 
     
    309309 
    310310    >>> f = ContactForm() 
    311     >>> f.clean_data 
     311    >>> f.cleaned_data 
    312312    Traceback (most recent call last): 
    313313    ... 
    314     AttributeError: 'ContactForm' object has no attribute 'clean_data' 
     314    AttributeError: 'ContactForm' object has no attribute 'cleaned_data' 
    315315 
    316316Outputting forms as HTML 
  • django/trunk/tests/modeltests/model_forms/models.py

    r5202 r5237  
    1919 
    2020The function django.newforms.save_instance() takes a bound form instance and a 
    21 model instance and saves the form's clean_data into the instance. It also takes 
     21model instance and saves the form's cleaned_data into the instance. It also takes 
    2222a commit=True parameter. 
    2323""" 
     
    9595>>> f.is_valid() 
    9696True 
    97 >>> f.clean_data 
     97>>> f.cleaned_data 
    9898{'url': u'entertainment', 'name': u'Entertainment'} 
    9999>>> obj = f.save() 
     
    106106>>> f.is_valid() 
    107107True 
    108 >>> f.clean_data 
     108>>> f.cleaned_data 
    109109{'url': u'test', 'name': u"It's a test"} 
    110110>>> obj = f.save() 
     
    120120>>> f.is_valid() 
    121121True 
    122 >>> f.clean_data 
     122>>> f.cleaned_data 
    123123{'url': u'third', 'name': u'Third test'} 
    124124>>> obj = f.save(commit=False) 
     
    135135>>> f.errors 
    136136{'name': [u'This field is required.']} 
    137 >>> f.clean_data 
    138 Traceback (most recent call last): 
    139 ... 
    140 AttributeError: 'CategoryForm' object has no attribute 'clean_data' 
     137>>> f.cleaned_data 
     138Traceback (most recent call last): 
     139... 
     140AttributeError: 'CategoryForm' object has no attribute 'cleaned_data' 
    141141>>> f.save() 
    142142Traceback (most recent call last): 
     
    525525>>> f.is_valid() 
    526526True 
    527 >>> f.clean_data 
     527>>> f.cleaned_data 
    528528{'phone': u'312-555-1212', 'description': u'Assistance'} 
    529529"""} 
  • django/trunk/tests/regressiontests/forms/regressions.py

    r5231 r5237  
    4747>>> f.is_valid() 
    4848True 
    49 >>> f.clean_data 
     49>>> f.cleaned_data 
    5050{'data': u'xyzzy'} 
    5151""" 
  • django/trunk/tests/regressiontests/forms/tests.py

    r5231 r5237  
    17751775>>> p.errors.as_text() 
    17761776u'' 
    1777 >>> p.clean_data 
     1777>>> p.cleaned_data 
    17781778{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 
    17791779>>> print p['first_name'] 
     
    18111811>>> p.is_valid() 
    18121812False 
    1813 >>> p.clean_data 
    1814 Traceback (most recent call last): 
    1815 ... 
    1816 AttributeError: 'Person' object has no attribute 'clean_data' 
     1813>>> p.cleaned_data 
     1814Traceback (most recent call last): 
     1815... 
     1816AttributeError: 'Person' object has no attribute 'cleaned_data' 
    18171817>>> print p 
    18181818<tr><th><label for="id_first_name">First name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="first_name" id="id_first_name" /></td></tr> 
     
    18451845>>> p.is_valid() 
    18461846False 
    1847 >>> p.clean_data 
    1848 Traceback (most recent call last): 
    1849 ... 
    1850 AttributeError: 'Person' object has no attribute 'clean_data' 
     1847>>> p.cleaned_data 
     1848Traceback (most recent call last): 
     1849... 
     1850AttributeError: 'Person' object has no attribute 'cleaned_data' 
    18511851>>> print p 
    18521852<tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr> 
     
    18871887* birthday 
    18881888  * This field is required. 
    1889 >>> p.clean_data 
    1890 Traceback (most recent call last): 
    1891 ... 
    1892 AttributeError: 'Person' object has no attribute 'clean_data' 
     1889>>> p.cleaned_data 
     1890Traceback (most recent call last): 
     1891... 
     1892AttributeError: 'Person' object has no attribute 'cleaned_data' 
    18931893>>> p['first_name'].errors 
    18941894[u'This field is required.'] 
     
    19061906<input type="text" name="birthday" id="id_birthday" /> 
    19071907 
    1908 clean_data will always *only* contain a key for fields defined in the 
     1908cleaned_data will always *only* contain a key for fields defined in the 
    19091909Form, even if you pass extra data when you define the Form. In this 
    19101910example, we pass a bunch of extra fields to the form constructor, 
    1911 but clean_data contains only the form's fields. 
     1911but cleaned_data contains only the form's fields. 
    19121912>>> data = {'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9', 'extra1': 'hello', 'extra2': 'hello'} 
    19131913>>> p = Person(data) 
    19141914>>> p.is_valid() 
    19151915True 
    1916 >>> p.clean_data 
     1916>>> p.cleaned_data 
    19171917{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 
    19181918 
    1919 clean_data will include a key and value for *all* fields defined in the Form, 
     1919cleaned_data will include a key and value for *all* fields defined in the Form, 
    19201920even if the Form's data didn't include a value for fields that are not 
    19211921required. In this example, the data dictionary doesn't include a value for the 
    1922 "nick_name" field, but clean_data includes it. For CharFields, it's set to the 
     1922"nick_name" field, but cleaned_data includes it. For CharFields, it's set to the 
    19231923empty string. 
    19241924>>> class OptionalPersonForm(Form): 
     
    19301930>>> f.is_valid() 
    19311931True 
    1932 >>> f.clean_data 
     1932>>> f.cleaned_data 
    19331933{'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 
    19341934 
     
    19421942>>> f.is_valid() 
    19431943True 
    1944 >>> f.clean_data 
     1944>>> f.cleaned_data 
    19451945{'birth_date': None, 'first_name': u'John', 'last_name': u'Lennon'} 
    19461946 
     
    22932293>>> f.errors 
    22942294{} 
    2295 >>> f.clean_data 
     2295>>> f.cleaned_data 
    22962296{'composers': [u'J'], 'name': u'Yesterday'} 
    22972297>>> f = SongForm({'name': 'Yesterday', 'composers': ['J', 'P']}, auto_id=False) 
    22982298>>> f.errors 
    22992299{} 
    2300 >>> f.clean_data 
     2300>>> f.cleaned_data 
    23012301{'composers': [u'J', u'P'], 'name': u'Yesterday'} 
    23022302 
     
    23042304>>> class EscapingForm(Form): 
    23052305...     special_name = CharField() 
    2306 ...     def do_clean_special_name(self): 
    2307 ...         raise ValidationError("Something's wrong with '%s'" % self.clean_data['special_name']) 
     2306...     def clean_special_name(self): 
     2307...         raise ValidationError("Something's wrong with '%s'" % self.cleaned_data['special_name']) 
    23082308 
    23092309>>> f = EscapingForm({'special_name': "Nothing to escape"}, auto_id=False) 
     
    23202320clean_XXX() method on the Form, where XXX is the field name. As in 
    23212321Field.clean(), the clean_XXX() method should return the cleaned value. In the 
    2322 clean_XXX() method, you have access to self.clean_data, which is a dictionary 
     2322clean_XXX() method, you have access to self.cleaned_data, which is a dictionary 
    23232323of all the data that has been cleaned *so far*, in order by the fields, 
    23242324including the current field (e.g., the field XXX if you're in clean_XXX()). 
     
    23272327...    password1 = CharField(widget=PasswordInput) 
    23282328...    password2 = CharField(widget=PasswordInput) 
    2329 ...    def do_clean_password2(self): 
    2330 ...        if self.clean_data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']: 
     2329...    def clean_password2(self): 
     2330...        if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: 
    23312331...            raise ValidationError(u'Please make sure your passwords match.') 
    2332 ...        return self.clean_data['password2'] 
     2332...        return self.cleaned_data['password2'] 
    23332333>>> f = UserRegistration(auto_id=False) 
    23342334>>> f.errors 
     
    23432343>>> f.errors 
    23442344{} 
    2345 >>> f.clean_data 
     2345>>> f.cleaned_data 
    23462346{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} 
    23472347 
     
    23502350method will not be associated with a particular field; it will have a 
    23512351special-case association with the field named '__all__'. 
    2352 Note that in Form.clean(), you have access to self.clean_data, a dictionary of 
     2352Note that in Form.clean(), you have access to self.cleaned_data, a dictionary of 
    23532353all the fields/values that have *not* raised a ValidationError. Also note 
    23542354Form.clean() is required to return a dictionary of all clean data. 
     
    23582358...    password2 = CharField(widget=PasswordInput) 
    23592359...    def clean(self): 
    2360 ...        if self.clean_data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']: 
     2360...        if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: 
    23612361...            raise ValidationError(u'Please make sure your passwords match.') 
    2362 ...        return self.clean_data 
     2362...        return self.cleaned_data 
    23632363>>> f = UserRegistration(auto_id=False) 
    23642364>>> f.errors 
     
    23872387>>> f.errors 
    23882388{} 
    2389 >>> f.clean_data 
     2389>>> f.cleaned_data 
    23902390{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} 
    23912391 
     
    29552955>>> p.is_valid() 
    29562956True 
    2957 >>> p.clean_data 
     2957>>> p.cleaned_data 
    29582958{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 
    29592959 
     
    29992999>>> p1.is_valid() 
    30003000True 
    3001 >>> p1.clean_data 
     3001>>> p1.cleaned_data 
    30023002{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 
    30033003>>> p2 = Person(data, prefix='person2') 
    30043004>>> p2.is_valid() 
    30053005True 
    3006 >>> p2.clean_data 
     3006>>> p2.cleaned_data 
    30073007{'first_name': u'Jim', 'last_name': u'Morrison', 'birthday': datetime.date(1943, 12, 8)} 
    30083008 
     
    30303030>>> p.is_valid() 
    30313031True 
    3032 >>> p.clean_data 
     3032>>> p.cleaned_data 
    30333033{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 
    30343034 
     
    30923092...    password2 = CharField(widget=PasswordInput) 
    30933093...    def clean(self): 
    3094 ...        if self.clean_data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']: 
     3094...        if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: 
    30953095...            raise ValidationError(u'Please make sure your passwords match.') 
    3096 ...        return self.clean_data 
     3096...        return self.cleaned_data 
    30973097>>> def my_function(method, post_data): 
    30983098...     if method == 'POST': 
     
    31013101...         form = UserRegistration(auto_id=False) 
    31023102...     if form.is_valid(): 
    3103 ...         return 'VALID: %r' % form.clean_data 
     3103...         return 'VALID: %r' % form.cleaned_data 
    31043104...     t = Template('<form action="" method="post">\n<table>\n{{ form }}\n</table>\n<input type="submit" />\n</form>') 
    31053105...     return t.render(Context({'form': form})) 
     
    31393139...    password2 = CharField(widget=PasswordInput) 
    31403140...    def clean(self): 
    3141 ...        if self.clean_data.get('password1') and self.clean_data.get('password2') and self.clean_data['password1'] != self.clean_data['password2']: 
     3141...        if self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and self.cleaned_data['password1'] != self.cleaned_data['password2']: 
    31423142...            raise ValidationError(u'Please make sure your passwords match.') 
    3143 ...        return self.clean_data 
     3143...        return self.cleaned_data 
    31443144 
    31453145You have full flexibility in displaying form fields in a template. Just pass a 
     
    34913491<input type="text" name="field1_2_0" value="2007-04-25" id="id_field1_2_0" /><input type="text" name="field1_2_1" value="06:24:00" id="id_field1_2_1" /></td></tr> 
    34923492 
    3493 >>> f.clean_data 
     3493>>> f.cleaned_data 
    34943494{'field1': u'some text,JP,2007-04-25 06:24:00'} 
    34953495