Django

Code

Show
Ignore:
Timestamp:
06/17/07 17:18:54 (2 years ago)
Author:
clong
Message:

per-object-permissions: Merged to trunk [5486] NOTE: Not fully tested, will be working on this over the next few weeks.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/per-object-permissions/docs/forms.txt

    r4242 r5488  
    1515that document to understand how we're making the switch. 
    1616 
    17 .. _newforms documentation: http://www.djangoproject.com/documentation/newforms/ 
     17.. _newforms documentation: ../newforms/ 
    1818 
    1919Introduction 
     
    174174        # Check for validation errors 
    175175        errors = manipulator.get_validation_errors(new_data) 
     176        manipulator.do_html2python(new_data) 
    176177        if errors: 
    177178            return render_to_response('places/errors.html', {'errors': errors}) 
    178179        else: 
    179             manipulator.do_html2python(new_data) 
    180180            new_place = manipulator.save(new_data) 
    181181            return HttpResponse("Place created: %s" % new_place) 
     
    230230            # Check for errors. 
    231231            errors = manipulator.get_validation_errors(new_data) 
     232            manipulator.do_html2python(new_data) 
    232233 
    233234            if not errors: 
    234235                # No errors. This means we can save the data! 
    235                 manipulator.do_html2python(new_data) 
    236236                new_place = manipulator.save(new_data) 
    237237 
     
    325325            new_data = request.POST.copy() 
    326326            errors = manipulator.get_validation_errors(new_data) 
     327            manipulator.do_html2python(new_data) 
    327328            if not errors: 
    328                 manipulator.do_html2python(new_data) 
    329329                manipulator.save(new_data) 
    330330 
     
    407407            new_data = request.POST.copy() 
    408408            errors = manipulator.get_validation_errors(new_data) 
     409            manipulator.do_html2python(new_data) 
    409410            if not errors: 
    410                 manipulator.do_html2python(new_data) 
    411411 
    412412                # Send e-mail using new_data here... 
     
    417417        form = forms.FormWrapper(manipulator, new_data, errors) 
    418418        return render_to_response('contact_form.html', {'form': form}) 
     419 
     420Implementing ``flatten_data`` for custom manipulators 
     421------------------------------------------------------ 
     422 
     423It is possible (although rarely needed) to replace the default automatically 
     424created manipulators on a model with your own custom manipulators. If you do 
     425this and you are intending to use those models in generic views, you should 
     426also define a ``flatten_data`` method in any ``ChangeManipulator`` replacement. 
     427This should act like the default ``flatten_data`` and return a dictionary 
     428mapping field names to their values, like so:: 
     429 
     430    def flatten_data(self): 
     431        obj = self.original_object 
     432        return dict( 
     433            from = obj.from, 
     434            subject = obj.subject, 
     435            ... 
     436        ) 
     437 
     438In this way, your new change manipulator will act exactly like the default 
     439version. 
    419440 
    420441``FileField`` and ``ImageField`` special cases 
     
    497518--------------------------- 
    498519 
    499 After a form has been submitted, Django first checks to see that all the 
    500 required fields are present and non-empty. For each field that passes that 
    501 test *and if the form submission contained data* for that field, all the 
    502 validators for that field are called in turn. The emphasized portion in the 
     520After a form has been submitted, Django validates each field in turn. First, 
     521if the field is required, Django checks that it is present and non-empty. Then, 
     522if that test passes *and the form submission contained data* for that field, all 
     523the validators for that field are called in turn. The emphasized portion in the 
    503524last sentence is important: if a form field is not submitted (because it 
    504525contains no data -- which is normal HTML behavior), the validators are not 
     
    547568    * isValidANSITime 
    548569    * isValidEmail 
     570    * isValidFloat 
    549571    * isValidImage 
    550572    * isValidImageURL 
     
    595617    against the current field. 
    596618 
     619``RequiredIfOtherFieldGiven`` 
     620    Takes a field name of the current field is only required if the other 
     621    field has a value. 
     622 
     623``RequiredIfOtherFieldsGiven`` 
     624    Similar to ``RequiredIfOtherFieldGiven``, except that it takes a list of 
     625    field names and if any one of the supplied fields has a value provided, 
     626    the current field being validated is required. 
     627 
    597628``RequiredIfOtherFieldNotGiven`` 
    598629    Takes the name of the other field and this field is only required if the 
    599630    other field has no value. 
    600  
    601 ``RequiredIfOtherFieldsNotGiven`` 
    602     Similar to ``RequiredIfOtherFieldNotGiven``, except that it takes a list 
    603     of field names and if any one of the supplied fields does not have a value 
    604     provided, the field being validated is required. 
    605631 
    606632``RequiredIfOtherFieldEquals`` and ``RequiredIfOtherFieldDoesNotEqual`` 
     
    608634    order). If the given field does (or does not have, in the latter case) the 
    609635    given value, then the current field being validated is required. 
     636 
     637    An optional ``other_label`` argument can be passed which, if given, is used 
     638    in error messages instead of the value. This allows more user friendly error 
     639    messages if the value itself is not descriptive enough. 
    610640 
    611641    Note that because validators are called before any ``do_html2python()`` 
     
    626656    Takes two boundary numbers, ``lower`` and ``upper``, and checks that the 
    627657    field is greater than ``lower`` (if given) and less than ``upper`` (if 
    628     given).   
    629      
     658    given). 
     659 
    630660    Both checks are inclusive. That is, ``NumberIsInRange(10, 20)`` will allow 
    631661    values of both 10 and 20. This validator only checks numeric values 
     
    636666    field being validated is a power of the integer. 
    637667 
    638 ``IsValidFloat`` 
     668``IsValidDecimal`` 
    639669    Takes a maximum number of digits and number of decimal places (in that 
    640     order) and validates whether the field is a float with less than the 
    641     maximum number of digits and decimal place
     670    order) and validates whether the field is a decimal with no more than the 
     671    maximum number of digits and decimal places
    642672 
    643673``MatchesRegularExpression`` 
     
    666696    document for more details). 
    667697 
    668 .. _`generic views`: http://www.djangoproject.com/documentation/generic_views/ 
    669 .. _`models API`: http://www.djangoproject.com/documentation/model_api/ 
    670 .. _settings: http://www.djangoproject.com/documentation/settings/ 
     698.. _`generic views`: ../generic_views/ 
     699.. _`models API`: ../model-api/ 
     700.. _settings: ../settings/