File: validation.rst

package info (click to toggle)
django-tastypie 0.9.10-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,020 kB
  • sloc: python: 6,530; makefile: 78; sh: 45
file content (102 lines) | stat: -rw-r--r-- 3,236 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
.. _ref-validation:

==========
Validation
==========

Validation allows you to ensure that the data being submitted by the user
is appropriate for storage. This can range from simple type checking on up
to complex validation that compares different fields together.

If the data is valid, an empty dictionary is returned and processing continues
as normal. If the data is invalid, a dictionary of error messages (keys being
the field names, values being a list of error messages). This will be
immediately returned to the user, serialized in the format they requested.

Usage
=====

Using these classes is simple. Simply provide them (or your own class) as a
``Meta`` option to the ``Resource`` in question. For example::

    from django.contrib.auth.models import User
    from tastypie.validation import Validation
    from tastypie.resources import ModelResource
    
    
    class UserResource(ModelResource):
        class Meta:
            queryset = User.objects.all()
            resource_name = 'auth/user'
            excludes = ['email', 'password', 'is_superuser']
            # Add it here.
            validation = Validation()


Validation Options
==================

Tastypie ships with the following ``Validation`` classes:

``Validation``
~~~~~~~~~~~~~~

The no-op validation option, the data submitted is always considered to be
valid.

This is the default class hooked up to ``Resource/ModelResource``.

``FormValidation``
~~~~~~~~~~~~~~~~~~

A more complex form of validation, this class accepts a ``form_class`` argument
to its constructor. You supply a Django ``Form`` (or ``ModelForm``, though
``save`` will never get called) and Tastypie will verify the ``data`` in the
``Bundle`` against the form.

.. warning::

    Data in the bundle must line up with the fieldnames in the ``Form``. If they
    do not, you'll need to either munge the data or change your form.

Usage looks like::

    from django import forms
    
    class NoteForm(forms.Form):
        title = forms.CharField(max_length=100)
        slug = forms.CharField(max_length=50)
        content = forms.CharField(required=False, widget=forms.Textarea)
        is_active = forms.BooleanField()
    
    form = FormValidation(form_class=NoteForm)


Implementing Your Own Validation
================================

Implementing your own ``Validation`` classes is a simple process. The
constructor can take whatever ``**kwargs`` it needs (if any). The only other
method to implement is the ``is_valid`` method::

    from tastypie.validation import Validation
    
    
    class AwesomeValidation(Validation):
        def is_valid(self, bundle, request=None):
            if not bundle.data:
                return {'__all__': 'Not quite what I had in mind.'}
            
            errors = {}
            
            for key, value in bundle.data.items():
                if not isinstance(value, basestring):
                    continue
                
                if not 'awesome' in value:
                    errors[key] = ['NOT ENOUGH AWESOME. NEEDS MORE.']
            
            return errors

Under this validation, every field that's a string is checked for the word
'awesome'. If it's not in the string, it's an error.