diff options
| author | Mark Aaron Shirley | 2012-11-21 09:37:22 -0800 | 
|---|---|---|
| committer | Mark Aaron Shirley | 2012-11-21 09:37:22 -0800 | 
| commit | 0876bed96304c3c2125e0de67736d40bfe921cf7 (patch) | |
| tree | 4ba4c0cceea3ca001665560619313aed1c9624b8 /docs/api-guide | |
| parent | 1adfc41dc7c7e50edbf72f87ebf62bae33eb212c (diff) | |
| parent | b0bad35ef0972ec26ff808d81b1f43f16683898d (diff) | |
| download | django-rest-framework-0876bed96304c3c2125e0de67736d40bfe921cf7.tar.bz2 | |
Merge remote-tracking branch 'upstream/master' into partial-update
Diffstat (limited to 'docs/api-guide')
| -rw-r--r-- | docs/api-guide/fields.md | 30 | ||||
| -rw-r--r-- | docs/api-guide/serializers.md | 2 | 
2 files changed, 31 insertions, 1 deletions
diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index d1c31ecc..1d4c34cb 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -153,6 +153,16 @@ A text representation, validates the text to be a valid e-mail address.  Corresponds to `django.db.models.fields.EmailField` +## RegexField + +A text representation, that validates the given value matches against a certain regular expression. + +Uses Django's `django.core.validators.RegexValidator` for validation. + +Corresponds to `django.forms.fields.RegexField` + +**Signature:** `RegexField(regex, max_length=None, min_length=None)` +  ## DateField  A date representation. @@ -324,5 +334,25 @@ This field is always read-only.  * `pk_url_kwarg` - The named url parameter for the pk field lookup. Default is `pk`.  * `slug_url_kwarg` - The named url parameter for the slug field lookup. Default is to use the same value as given for `slug_field`. +# Other Fields + +## SerializerMethodField + +This is a read-only field. It gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object. The field's constructor accepts a single argument, which is the name of the method on the serializer to be called. The method should accept a single argument (in addition to `self`), which is the object being serialized. It should return whatever you want to be included in the serialized representation of the object. For example: + +    from rest_framework import serializers +    from django.contrib.auth.models import User +    from django.utils.timezone import now + +    class UserSerializer(serializers.ModelSerializer): + +        days_since_joined = serializers.SerializerMethodField('get_days_since_joined') + +        class Meta: +            model = User + +        def get_days_since_joined(self, obj): +            return (now() - obj.date_joined).days +  [cite]: http://www.python.org/dev/peps/pep-0020/  [FILE_UPLOAD_HANDLERS]: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FILE_UPLOAD_HANDLERS diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index 624c4159..19efde3c 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -34,7 +34,7 @@ Declaring a serializer looks very similar to declaring a form:          created = serializers.DateTimeField()          def restore_object(self, attrs, instance=None): -            if instance: +            if instance is not None:                  instance.title = attrs['title']                  instance.content = attrs['content']                  instance.created = attrs['created']  | 
