django-rest-framework/docs/api-guide/serializers.md

912 lines
42 KiB
Markdown
Raw Normal View History

source: serializers.py
2012-09-09 01:06:13 +04:00
2012-09-01 23:26:27 +04:00
# Serializers
> Expanding the usefulness of the serializers is something that we would
2013-05-28 19:13:12 +04:00
like to address. However, it's not a trivial problem, and it
2012-12-29 16:15:15 +04:00
will take some serious design work.
2012-09-01 23:26:27 +04:00
>
> — Russell Keith-Magee, [Django users group][cite]
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into `JSON`, `XML` or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
2012-09-01 23:26:27 +04:00
2014-11-27 11:09:58 +03:00
The serializers in REST framework work very similarly to Django's `Form` and `ModelForm` classes. We provide a `Serializer` class which gives you a powerful, generic way to control the output of your responses, as well as a `ModelSerializer` class which provides a useful shortcut for creating serializers that deal with model instances and querysets.
2012-09-01 23:26:27 +04:00
## Declaring Serializers
Let's start by creating a simple object we can use for example purposes:
class Comment(object):
def __init__(self, email, content, created=None):
self.email = email
self.content = content
self.created = created or datetime.datetime.now()
2012-09-01 23:26:27 +04:00
comment = Comment(email='leila@example.com', content='foo bar')
2014-11-27 11:09:58 +03:00
We'll declare a serializer that we can use to serialize and deserialize data that corresponds to `Comment` objects.
2013-03-17 23:59:13 +04:00
2012-09-01 23:26:27 +04:00
Declaring a serializer looks very similar to declaring a form:
from rest_framework import serializers
2012-09-01 23:26:27 +04:00
class CommentSerializer(serializers.Serializer):
email = serializers.EmailField()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
## Serializing objects
2014-11-27 11:09:58 +03:00
We can now use `CommentSerializer` to serialize a comment, or list of comments. Again, using the `Serializer` class looks a lot like using a `Form` class.
2012-09-01 23:26:27 +04:00
serializer = CommentSerializer(comment)
2012-09-01 23:26:27 +04:00
serializer.data
# {'email': u'leila@example.com', 'content': u'foo bar', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774)}
At this point we've translated the model instance into Python native datatypes. To finalise the serialization process we render the data into `json`.
2012-09-01 23:26:27 +04:00
from rest_framework.renderers import JSONRenderer
json = JSONRenderer().render(serializer.data)
json
2012-09-01 23:26:27 +04:00
# '{"email": "leila@example.com", "content": "foo bar", "created": "2012-08-22T16:20:09.822"}'
## Deserializing objects
2014-11-27 11:09:58 +03:00
Deserialization is similar. First we parse a stream into Python native datatypes...
2012-09-01 23:26:27 +04:00
from StringIO import StringIO
from rest_framework.parsers import JSONParser
stream = StringIO(json)
2012-09-01 23:26:27 +04:00
data = JSONParser().parse(stream)
2014-11-27 11:09:58 +03:00
...then we restore those native datatypes into a dictionary of validated data.
2012-09-01 23:26:27 +04:00
serializer = CommentSerializer(data=data)
2012-09-01 23:26:27 +04:00
serializer.is_valid()
# True
2014-11-27 11:09:58 +03:00
serializer.validated_data
# {'content': 'foo bar', 'email': 'leila@example.com', 'created': datetime.datetime(2012, 08, 22, 16, 20, 09, 822243)}
## Saving instances
If we want to be able to return complete object instances based on the validated data we need to implement one or both of the `.create()` and `update()` methods. For example:
class CommentSerializer(serializers.Serializer):
email = serializers.EmailField()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
def create(self, validated_data):
return Comment(**validated_data)
def update(self, instance, validated_data):
instance.email = validated_data.get('email', instance.email)
instance.content = validated_data.get('content', instance.content)
instance.created = validated_data.get('created', instance.created)
return instance
If your object instances correspond to Django models you'll also want to ensure that these methods save the object to the database. For example, if `Comment` was a Django model, the methods might look like this:
def create(self, validated_data):
return Comment.objcts.create(**validated_data)
def update(self, instance, validated_data):
instance.email = validated_data.get('email', instance.email)
instance.content = validated_data.get('content', instance.content)
instance.created = validated_data.get('created', instance.created)
instance.save()
return instance
Now when deserializing data, we can call `.save()` to return an object instance, based on the validated data.
comment = serializer.save()
Calling `.save()` will either create a new instance, or update an existing instance, depending on if an existing instance was passed when instantiating the serializer class:
# .save() will create a new instance.
serializer = CommentSerializer(data=data)
# .save() will update the existing `comment` instance.
serializer = CommentSerializer(comment, data=data)
2012-09-01 23:26:27 +04:00
2014-11-27 11:09:58 +03:00
Both the `.create()` and `.update()` methods are optional. You can implement either neither, one, or both of them, depending on the use-case for your serializer class.
2014-11-27 11:09:58 +03:00
#### Passing additional attributes to `.save()`
2014-11-27 11:09:58 +03:00
Sometimes you'll want your view code to be able to inject additional data at the point of saving the instance. This additional data might include information like the current user, the current time, or anything else that is not part of the request data.
2014-11-27 11:09:58 +03:00
You can do so by including additional keyword arguments when calling `.save()`. For example:
serializer.save(owner=request.user)
Any additional keyword arguments will be included in the `validated_data` argument when `.create()` or `.update()` are called.
#### Overriding `.save()` directly.
In some cases the `.create()` and `.update()` method names may not be meaningful. For example, in a contact form we may not be creating new instances, but instead sending an email or other message.
In these cases you might instead choose to override `.save()` directly, as being more readable and meaningful.
For example:
class ContactForm(serializers.Serializer):
email = serializers.EmailField()
message = serializers.CharField()
def save(self):
email = self.validated_data['email']
message = self.validated_data['message']
send_email(from=email, message=message)
Note that in the case above we're now having to access the serializer `.validated_data` property directly.
2012-09-01 23:26:27 +04:00
## Validation
2014-11-27 11:09:58 +03:00
When deserializing data, you always need to call `is_valid()` before attempting to access the validated data, or save an object instance. If any validation errors occur, the `.errors` property will contain a dictionary representing the resulting error messages. For example:
serializer = CommentSerializer(data={'email': 'foobar', 'content': 'baz'})
serializer.is_valid()
# False
serializer.errors
# {'email': [u'Enter a valid e-mail address.'], 'created': [u'This field is required.']}
2013-03-19 18:26:48 +04:00
2014-11-27 11:09:58 +03:00
Each key in the dictionary will be the field name, and the values will be lists of strings of any error messages corresponding to that field. The `non_field_errors` key may also be present, and will list any general validation errors. The name of the `non_field_errors` key may be customized using the `NON_FIELD_ERRORS_KEY` REST framework setting.
2012-09-01 23:26:27 +04:00
When deserializing a list of items, errors will be returned as a list of dictionaries representing each of the deserialized items.
2014-11-27 11:09:58 +03:00
#### Raising an exception on invalid data
The `.is_valid()` method takes an optional `raise_exception` flag that will cause it to raise a `serializers.ValidationError` exception if there are validation errors.
These exceptions are automatically dealt with by the default exception handler that REST framework provides, and will return `HTTP 400 Bad Request` responses by default.
# Return a 400 response if the data was invalid.
serializer.is_valid(raise_exception=True)
#### Field-level validation
2014-11-28 12:56:44 +03:00
You can specify custom field-level validation by adding `.validate_<field_name>` methods to your `Serializer` subclass. These are similar to the `.clean_<field_name>` methods on Django forms.
2014-11-27 11:09:58 +03:00
These methods take a single argument, which is the field value that requires validation.
2014-11-28 12:56:44 +03:00
Your `validate_<field_name>` methods should return the validated value or raise a `serializers.ValidationError`. For example:
from rest_framework import serializers
class BlogPostSerializer(serializers.Serializer):
title = serializers.CharField(max_length=100)
content = serializers.CharField()
2014-11-27 11:09:58 +03:00
def validate_title(self, value):
"""
Check that the blog post is about Django.
"""
2014-11-27 11:09:58 +03:00
if 'django' not in value.lower():
raise serializers.ValidationError("Blog post is not about Django")
2014-11-27 11:09:58 +03:00
return value
#### Object-level validation
2014-11-27 11:09:58 +03:00
To do any other validation that requires access to multiple fields, add a method called `.validate()` to your `Serializer` subclass. This method takes a single argument, which is a dictionary of field values. It should raise a `ValidationError` if necessary, or just return the validated values. For example:
2012-12-28 16:59:24 +04:00
from rest_framework import serializers
class EventSerializer(serializers.Serializer):
2013-02-12 17:55:00 +04:00
description = serializers.CharField(max_length=100)
2012-12-28 16:59:24 +04:00
start = serializers.DateTimeField()
finish = serializers.DateTimeField()
2014-11-27 11:09:58 +03:00
def validate(self, data):
2012-12-28 16:59:24 +04:00
"""
Check that the start is before the stop.
"""
2014-11-27 11:09:58 +03:00
if data['start'] > data['finish']:
2012-12-28 16:59:24 +04:00
raise serializers.ValidationError("finish must occur after start")
2014-11-27 11:09:58 +03:00
return data
#### Validators
Individual fields on a serializer can include validators, by declaring them on the field instance, for example:
def multiple_of_ten(value):
if value % 10 != 0:
raise serializers.ValidationError('Not a multiple of ten')
2014-11-27 11:09:58 +03:00
class GameRecord(serializers.Serializer):
score = IntegerField(validators=[multiple_of_ten])
...
2014-11-27 11:09:58 +03:00
Serializer classes can also include reusable validators that are applied to the complete set of field data. These validators are included by declaring them on an inner `Meta` class, like so:
class EventSerializer(serializers.Serializer):
name = serializers.CharField()
room_number = serializers.IntegerField(choices=[101, 102, 103, 201])
date = serializers.DateField()
class Meta:
# Each room only has one event per day.
validators = UniqueTogetherValidator(
queryset=Event.objects.all(),
fields=['room_number', 'date']
)
2014-11-27 11:09:58 +03:00
For more information see the [validators documentation](validators.md).
2014-11-27 11:09:58 +03:00
## Partial updates
2012-10-24 14:39:17 +04:00
2014-11-27 11:09:58 +03:00
By default, serializers must be passed values for all required fields or they will raise validation errors. You can use the `partial` argument in order to allow partial updates.
# Update `comment` with partial data
serializer = CommentSerializer(comment, data={'content': u'foo bar'}, partial=True)
2012-10-24 14:39:17 +04:00
2012-09-01 23:26:27 +04:00
## Dealing with nested objects
The previous examples are fine for dealing with objects that only have simple datatypes, but sometimes we also need to be able to represent more complex objects, where some of the attributes of an object might not be simple datatypes such as strings, dates or integers.
2012-09-01 23:26:27 +04:00
The `Serializer` class is itself a type of `Field`, and can be used to represent relationships where one object type is nested inside another.
class UserSerializer(serializers.Serializer):
email = serializers.EmailField()
username = serializers.CharField(max_length=100)
2012-09-01 23:26:27 +04:00
class CommentSerializer(serializers.Serializer):
user = UserSerializer()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
If a nested representation may optionally accept the `None` value you should pass the `required=False` flag to the nested serializer.
class CommentSerializer(serializers.Serializer):
user = UserSerializer(required=False) # May be an anonymous user.
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
2013-07-15 05:13:37 +04:00
Similarly if a nested representation should be a list of items, you should pass the `many=True` flag to the nested serialized.
class CommentSerializer(serializers.Serializer):
user = UserSerializer(required=False)
edits = EditItemSerializer(many=True) # A nested list of 'edit' items.
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
2012-10-29 00:43:43 +04:00
2013-07-15 05:13:37 +04:00
Validation of nested objects will work the same as before. Errors with nested objects will be nested under the field name of the nested object.
2012-10-29 00:43:43 +04:00
2014-01-10 17:22:47 +04:00
serializer = CommentSerializer(data={'user': {'email': 'foobar', 'username': 'doe'}, 'content': 'baz'})
2013-07-15 05:13:37 +04:00
serializer.is_valid()
# False
serializer.errors
# {'user': {'email': [u'Enter a valid e-mail address.']}, 'created': [u'This field is required.']}
2012-10-29 00:43:43 +04:00
2014-11-27 11:09:58 +03:00
**TODO** Document create and update for nested serializers
## Dealing with multiple objects
2013-02-12 17:55:00 +04:00
The `Serializer` class can also handle serializing or deserializing lists of objects.
2013-02-12 17:55:00 +04:00
#### Serializing multiple objects
2013-02-12 17:55:00 +04:00
To serialize a queryset or list of objects instead of a single object instance, you should pass the `many=True` flag when instantiating the serializer. You can then pass a queryset or list of objects to be serialized.
queryset = Book.objects.all()
serializer = BookSerializer(queryset, many=True)
2013-02-12 17:55:00 +04:00
serializer.data
# [
# {'id': 0, 'title': 'The electric kool-aid acid test', 'author': 'Tom Wolfe'},
# {'id': 1, 'title': 'If this is a man', 'author': 'Primo Levi'},
# {'id': 2, 'title': 'The wind-up bird chronicle', 'author': 'Haruki Murakami'}
# ]
2013-02-12 17:55:00 +04:00
#### Deserializing multiple objects for creation
2012-09-01 23:26:27 +04:00
2014-11-27 11:09:58 +03:00
**TODO**
To deserialize a list of object data, and create multiple object instances in a single pass, you should also set the `many=True` flag, and pass a list of data to be deserialized.
2012-09-01 23:26:27 +04:00
This allows you to write views that create multiple items when a `POST` request is made.
2012-09-01 23:26:27 +04:00
For example:
2012-09-01 23:26:27 +04:00
data = [
{'title': 'The bell jar', 'author': 'Sylvia Plath'},
{'title': 'For whom the bell tolls', 'author': 'Ernest Hemingway'}
]
serializer = BookSerializer(data=data, many=True)
serializer.is_valid()
# True
serializer.save() # `.save()` will be called on each deserialized instance
2014-11-28 12:56:44 +03:00
The default implementation for multiple object creation is to simply call `.create()` for each item in the list. If you want to customize this behavior, you'll need to customize the `.create()` method on `ListSerializer` class that is used when `many=True` is passed.
For example:
class BookListSerializer(serializers.ListSerializer):
def create(self, validated_data):
books = [Book(**item) for item in validated_data]
return Book.objects.bulk_create(books)
class BookSerializer(serializers.Serializer):
class Meta:
list_serializer_class = BookListSerializer
#### Deserializing multiple objects for update
2014-11-27 11:09:58 +03:00
**TODO**
You can also deserialize a list of objects as part of a bulk update of multiple existing items.
In this case you need to supply both an existing list or queryset of items, as well as a list of data to update those items with.
This allows you to write views that update or create multiple items when a `PUT` request is made.
# Capitalizing the titles of the books
queryset = Book.objects.all()
data = [
{'id': 3, 'title': 'The Bell Jar', 'author': 'Sylvia Plath'},
{'id': 4, 'title': 'For Whom the Bell Tolls', 'author': 'Ernest Hemingway'}
]
serializer = BookSerializer(queryset, data=data, many=True)
serializer.is_valid()
# True
2013-09-04 14:38:34 +04:00
serializer.save() # `.save()` will be called on each updated or newly created instance.
2013-03-26 00:26:34 +04:00
By default bulk updates will be limited to updating instances that already exist in the provided queryset.
2013-03-26 00:26:34 +04:00
When performing a bulk update you may want to allow new items to be created, and missing items to be deleted. To do so, pass `allow_add_remove=True` to the serializer.
2013-03-26 00:26:34 +04:00
serializer = BookSerializer(queryset, data=data, many=True, allow_add_remove=True)
serializer.is_valid()
# True
2013-03-26 00:26:34 +04:00
serializer.save() # `.save()` will be called on updated or newly created instances.
# `.delete()` will be called on any other items in the `queryset`.
Passing `allow_add_remove=True` ensures that any update operations will completely overwrite the existing queryset, rather than simply updating existing objects.
## Including extra context
There are some cases where you need to provide extra context to the serializer in addition to the object being serialized. One common case is if you're using a serializer that includes hyperlinked relations, which requires the serializer to have access to the current request so that it can properly generate fully qualified URLs.
2013-05-28 19:13:12 +04:00
You can provide arbitrary additional context by passing a `context` argument when instantiating the serializer. For example:
serializer = AccountSerializer(account, context={'request': request})
serializer.data
# {'id': 6, 'owner': u'denvercoder9', 'created': datetime.datetime(2013, 2, 12, 09, 44, 56, 678870), 'details': 'http://example.com/accounts/6/details'}
2014-11-27 11:09:58 +03:00
The context dictionary can be used within any serializer field logic, such as a custom `.to_representation()` method, by accessing the `self.context` attribute.
---
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
# ModelSerializer
2012-09-01 23:26:27 +04:00
2014-11-27 11:09:58 +03:00
Often you'll want serializer classes that map closely to Django model definitions.
The `ModelSerializer` class provides a shortcut that lets you automatically create a `Serializer` class with fields that correspond to the Model fields.
**The `ModelSerializer` class is the same as a regular `Serializer` class, except that**:
* It will automatically generate a set of fields for you, based on the model.
* It will automatically generate validators for the serializer, such as unique_together validators.
* It includes simple default implementations of `.create()` and `.update()`.
Declaring a `ModelSerializer` looks like this:
2012-09-01 23:26:27 +04:00
2012-10-09 20:49:04 +04:00
class AccountSerializer(serializers.ModelSerializer):
2012-09-01 23:26:27 +04:00
class Meta:
model = Account
2014-11-27 11:09:58 +03:00
By default, all the model fields on the class will be mapped to a corresponding serializer fields.
2013-02-24 02:01:29 +04:00
2014-11-27 11:09:58 +03:00
Any relationships such as foreign keys on the model will be mapped to `PrimaryKeyRelatedField`. Reverse relationships are not included by default unless explicitly included as described below.
2013-05-05 19:48:12 +04:00
2014-11-27 11:09:58 +03:00
#### Inspecting the generated `ModelSerializer` class.
2014-11-27 11:09:58 +03:00
Serializer classes generate helpful verbose representation strings, that allow you to fully inspect the state of their fields. This is particularly useful when working with `ModelSerializers` where you want to determine what set of fields and validators are being automatically created for you.
2014-11-27 11:09:58 +03:00
To do so, open the Django shell, using `python manage.py shell`, then import the serializer class, instantiate it, and print the object representation…
2014-11-27 11:09:58 +03:00
>>> from myapp.serializers import AccountSerializer
>>> serializer = AccountSerializer()
>>> print repr(serializer) # Or `print(repr(serializer))` in Python 3.x.
AccountSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(allow_blank=True, max_length=100, required=False)
owner = PrimaryKeyRelatedField(queryset=User.objects.all())
2013-05-05 19:48:12 +04:00
## Specifying which fields should be included
If you only want a subset of the default fields to be used in a model serializer, you can do so using `fields` or `exclude` options, just as you would with a `ModelForm`.
For example:
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ('id', 'account_name', 'users', 'created')
2014-11-27 11:09:58 +03:00
The names in the `fields` option will normally map to model fields on the model class.
Alternatively names in the `fields` options can map to properties or methods which take no arguments that exist on the model class.
2013-05-05 19:48:12 +04:00
## Specifying nested serialization
The default `ModelSerializer` uses primary keys for relationships, but you can also easily generate nested representations using the `depth` option:
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ('id', 'account_name', 'users', 'created')
depth = 1
The `depth` option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation.
2013-07-15 05:13:37 +04:00
If you want to customize the way the serialization is done (e.g. using `allow_add_remove`) you'll need to define the field yourself.
2014-11-27 11:09:58 +03:00
## Specifying fields explicitly
You can add extra fields to a `ModelSerializer` or override the default fields by declaring fields on the class, just as you would for a `Serializer` class.
class AccountSerializer(serializers.ModelSerializer):
url = serializers.CharField(source='get_absolute_url', read_only=True)
groups = serializers.PrimaryKeyRelatedField(many=True)
class Meta:
model = Account
Extra fields can correspond to any property or callable on the model.
## Specifying which fields should be read-only
2013-05-05 19:48:12 +04:00
2014-11-27 11:09:58 +03:00
You may wish to specify multiple fields as read-only. Instead of adding each field explicitly with the `read_only=True` attribute, you may use the shortcut Meta option, `read_only_fields`.
This option should be a list or tuple of field names, and is declared as follows:
2013-05-05 19:48:12 +04:00
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ('id', 'account_name', 'users', 'created')
read_only_fields = ('account_name',)
Model fields which have `editable=False` set, and `AutoField` fields will be set to read-only by default, and do not need to be added to the `read_only_fields` option.
2012-09-01 23:26:27 +04:00
2014-11-27 11:09:58 +03:00
## Specifying additional keyword arguments for fields.
There is also a shortcut allowing you to specify arbitrary additional keyword arguments on fields, using the `extra_kwargs` option. Similarly to `read_only_fields` this means you do not need to explicitly declare the field on the serializer.
2014-11-27 11:09:58 +03:00
This option is a dictionary, mapping field names to a dictionary of keyword arguments. For example:
class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email', 'username', 'password')
2014-11-27 11:09:58 +03:00
extra_kwargs = {'password': {'write_only': True}}
def create(self, validated_data):
user = User(
email=validated_data['email'],
username=validated_data['username']
)
user.set_password(validated_data['password'])
user.save()
return user
2012-09-01 23:26:27 +04:00
## Relational fields
2013-02-24 02:01:29 +04:00
When serializing model instances, there are a number of different ways you might choose to represent relationships. The default representation for `ModelSerializer` is to use the primary keys of the related instances.
2012-09-01 23:26:27 +04:00
2013-02-24 02:01:29 +04:00
Alternative representations include serializing using hyperlinks, serializing complete nested representations, or serializing with a custom representation.
2012-09-01 23:26:27 +04:00
2013-02-24 02:01:29 +04:00
For full details see the [serializer relations][relations] documentation.
2012-09-01 23:26:27 +04:00
## Inheritance of the 'Meta' class
2014-11-27 11:09:58 +03:00
The inner `Meta` class on serializers is not inherited from parent classes by default. This is the same behavior as with Django's `Model` and `ModelForm` classes. If you want the `Meta` class to inherit from a parent class you must do so explicitly. For example:
class AccountSerializer(MyBaseSerializer):
class Meta(MyBaseSerializer.Meta):
model = Account
Typically we would recommend *not* using inheritance on inner Meta classes, but instead declaring all options explicitly.
2013-05-05 19:48:12 +04:00
---
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
# HyperlinkedModelSerializer
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
The `HyperlinkedModelSerializer` class is similar to the `ModelSerializer` class except that it uses hyperlinks to represent relationships, rather than primary keys.
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
By default the serializer will include a `url` field instead of a primary key field.
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
The url field will be represented using a `HyperlinkedIdentityField` serializer field, and any relationships on the model will be represented using a `HyperlinkedRelatedField` serializer field.
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
You can explicitly include the primary key by adding it to the `fields` option, for example:
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
class AccountSerializer(serializers.HyperlinkedModelSerializer):
2012-09-01 23:26:27 +04:00
class Meta:
model = Account
2013-05-05 19:48:12 +04:00
fields = ('url', 'id', 'account_name', 'users', 'created')
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
## How hyperlinked views are determined
2012-09-01 23:26:27 +04:00
2013-05-05 19:48:12 +04:00
There needs to be a way of determining which views should be used for hyperlinking to model instances.
2013-05-05 19:48:12 +04:00
By default hyperlinks are expected to correspond to a view name that matches the style `'{model_name}-detail'`, and looks up the instance by a `pk` keyword argument.
2014-11-27 11:09:58 +03:00
You can change the field that is used for object lookups by setting the `lookup_field` option. The value of this option should correspond both with a kwarg in the URL conf, and with a field on the model. For example:
2013-05-05 19:48:12 +04:00
class AccountSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Account
2013-05-05 19:48:12 +04:00
fields = ('url', 'account_name', 'users', 'created')
lookup_field = 'slug'
2013-12-09 13:53:16 +04:00
Note that the `lookup_field` will be used as the default on *all* hyperlinked fields, including both the URL identity, and any hyperlinked relationships.
2013-08-07 22:00:06 +04:00
For more specific requirements such as specifying a different lookup for each field, you'll want to set the fields on the serializer explicitly. For example:
2013-05-05 19:48:12 +04:00
class AccountSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
2014-11-27 11:09:58 +03:00
view_name='account-detail',
lookup_field='slug'
2013-05-05 19:48:12 +04:00
)
users = serializers.HyperlinkedRelatedField(
view_name='user-detail',
lookup_field='username',
many=True,
read_only=True
)
class Meta:
model = Account
fields = ('url', 'account_name', 'users', 'created')
2014-05-15 16:34:31 +04:00
## Overriding the URL field behavior
2014-01-15 00:34:33 +04:00
The name of the URL field defaults to 'url'. You can override this globally, by using the `URL_FIELD_NAME` setting.
You can also override this on a per-serializer basis by using the `url_field_name` option on the serializer, like so:
class AccountSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Account
fields = ('account_url', 'account_name', 'users', 'created')
url_field_name = 'account_url'
2014-11-27 11:09:58 +03:00
**Note**: The generic view implementations normally generate a `Location` header in response to successful `POST` requests. Serializers using `url_field_name` option will not have this header automatically included by the view. If you need to do so you will ned to also override the view's `get_success_headers()` method.
2014-01-15 00:34:33 +04:00
2014-05-15 16:34:31 +04:00
You can also override the URL field's view name and lookup field without overriding the field explicitly, by using the `view_name` and `lookup_field` options, like so:
2014-01-15 00:34:33 +04:00
class AccountSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Account
fields = ('account_url', 'account_name', 'users', 'created')
view_name = 'account_detail'
lookup_field='account_name'
2013-05-05 19:48:12 +04:00
---
2014-11-28 12:56:44 +03:00
# ListSerializer
The `ListSerializer` class provides the behavior for serializing and validating multiple objects at once. You won't *typically* need to use `ListSerializer` directly, but should instead simply pass `many=True` when instantiating a serializer.
When a serializer is instantiated and `many=True` is passed, a `ListSerializer` instance will be created. The serializer class then becomes a child of the parent `ListSerializer`
There *are* a few use cases when you might want to customize the `ListSerializer` behavior. For example:
* You want to provide particular validation of the lists, such as always ensuring that there is at least one element in a list.
* You want to customize the create or update behavior of multiple objects.
For these cases you can modify the class that is used when `many=True` is passed, by using the `list_serializer_class` option on the serializer `Meta` class.
For example:
class CustomListSerializer(serializers.ListSerializer):
...
class CustomSerializer(serializers.Serializer):
...
class Meta:
list_serializer_class = CustomListSerializer
#### Customizing `.create()` for multiple objects.
The default implementation for multiple object creation is to simply call `.create()` for each item in the list. If you want to customize this behavior, you'll need to customize the `.create()` method on `ListSerializer` class that is used when `many=True` is passed.
For example:
class BookListSerializer(serializers.ListSerializer):
def create(self, validated_data):
books = [Book(**item) for item in validated_data]
return Book.objects.bulk_create(books)
class BookSerializer(serializers.Serializer):
...
class Meta:
list_serializer_class = BookListSerializer
#### Customizing `.update()` for multiple objects.
By default the `ListSerializer` class does not support multiple updates. This is because the behavior that should be expected for insertions and deletions is ambiguous.
To support multiple updates you'll need to do so explicitly. When writing your multiple update code make sure to keep the following in mind:
* How do you determine which instance should be updated for each item in the list of data?
* How should insertions be handled? Are they invalid, or do they create new objects?
* How should removals be handled? Do they imply object deletion, or removing a relationship? Should they be silently ignored, or are they invalid?
* How should ordering be handled? Does changing the position of two items imply any state change or is it ignored?
Here's an example of how you might choose to implement multiple updates:
class BookListSerializer(serializers.ListSerializer):
def update(self, instance, validated_data):
# Maps for id->instance and id->data item.
book_mapping = {book.id: book for book in instance}
data_mapping = {item['id']: item for item in validated_data}
# Perform creations and updates.
ret = []
for book_id, data in data_mapping.items():
book = book_mapping.get(book_id, None):
if book is None:
ret.append(self.child.create(data))
else:
ret.append(self.child.update(book, data))
# Perform deletions.
for book_id, book in book_mapping.items():
if book_id not in data_mapping:
book.delete()
return ret
class BookSerializer(serializers.Serializer):
...
class Meta:
list_serializer_class = BookListSerializer
---
# BaseSerializer
`BaseSerializer` class that can be used to easily support alternative serialization and deserialization styles.
This class implements the same basic API as the `Serializer` class:
* `.data` - Returns the outgoing primitive representation.
* `.is_valid()` - Deserializes and validates incoming data.
* `.validated_data` - Returns the validated incoming data.
* `.errors` - Returns an errors during validation.
* `.save()` - Persists the validated data into an object instance.
There are four methods that can be overridden, depending on what functionality you want the serializer class to support:
* `.to_representation()` - Override this to support serialization, for read operations.
* `.to_internal_value()` - Override this to support deserialization, for write operations.
* `.create()` and `.update()` - Overide either or both of these to support saving instances.
Because this class provides the same interface as the `Serializer` class, you can use it with the existing generic class based views exactly as you would for a regular `Serializer` or `ModelSerializer`.
The only difference you'll notice when doing so is the `BaseSerializer` classes will not generate HTML forms in the browsable API. This is because the data they return does not include all the field information that would allow each field to be rendered into a suitable HTML input.
##### Read-only `BaseSerializer` classes.
To implement a read-only serializer using the `BaseSerializer` class, we just need to override the `.to_representation()` method. Let's take a look at an example using a simple Django model:
class HighScore(models.Model):
created = models.DateTimeField(auto_now_add=True)
player_name = models.CharField(max_length=10)
score = models.IntegerField()
It's simple to create a read-only serializer for converting `HighScore` instances into primitive data types.
class HighScoreSerializer(serializers.BaseSerializer):
def to_representation(self, obj):
return {
'score': obj.score,
'player_name': obj.player_name
}
We can now use this class to serialize single `HighScore` instances:
@api_view(['GET'])
def high_score(request, pk):
instance = HighScore.objects.get(pk=pk)
serializer = HighScoreSerializer(instance)
return Response(serializer.data)
Or use it to serialize multiple instances:
@api_view(['GET'])
def all_high_scores(request):
queryset = HighScore.objects.order_by('-score')
serializer = HighScoreSerializer(queryset, many=True)
return Response(serializer.data)
##### Read-write `BaseSerializer` classes.
To create a read-write serializer we first need to implement a `.to_internal_value()` method. This method returns the validated values that will be used to construct the object instance, and may raise a `ValidationError` if the supplied data is in an incorrect format.
Once you've implemented `.to_internal_value()`, the basic validation API will be available on the serializer, and you will be able to use `.is_valid()`, `.validated_data` and `.errors`.
If you want to also support `.save()` you'll need to also implement either or both of the `.create()` and `.update()` methods.
Here's a complete example of our previous `HighScoreSerializer`, that's been updated to support both read and write operations.
class HighScoreSerializer(serializers.BaseSerializer):
def to_internal_value(self, data):
score = data.get('score')
player_name = data.get('player_name')
# Perform the data validation.
if not score:
raise ValidationError({
'score': 'This field is required.'
})
if not player_name:
raise ValidationError({
'player_name': 'This field is required.'
})
if len(player_name) > 10:
raise ValidationError({
'player_name': 'May not be more than 10 characters.'
})
# Return the validated values. This will be available as
# the `.validated_data` property.
return {
'score': int(score),
'player_name': player_name
}
def to_representation(self, obj):
return {
'score': obj.score,
'player_name': obj.player_name
}
def create(self, validated_data):
return HighScore.objects.create(**validated_data)
#### Creating new generic serializers with `BaseSerializer`.
The `BaseSerializer` class is also useful if you want to implement new generic serializer classes for dealing with particular serialization styles, or for integrating with alternative storage backends.
The following class is an example of a generic serializer that can handle coercing arbitrary objects into primitive representations.
class ObjectSerializer(serializers.BaseSerializer):
"""
A read-only serializer that coerces arbitrary complex objects
into primitive representations.
"""
def to_representation(self, obj):
for attribute_name in dir(obj):
attribute = getattr(obj, attribute_name)
if attribute_name('_'):
# Ignore private attributes.
pass
elif hasattr(attribute, '__call__'):
# Ignore methods and other callables.
pass
elif isinstance(attribute, (str, int, bool, float, type(None))):
# Primitive types can be passed through unmodified.
output[attribute_name] = attribute
elif isinstance(attribute, list):
# Recursively deal with items in lists.
output[attribute_name] = [
self.to_representation(item) for item in attribute
]
elif isinstance(attribute, dict):
# Recursively deal with items in dictionaries.
output[attribute_name] = {
str(key): self.to_representation(value)
for key, value in attribute.items()
}
else:
# Force anything else to its string representation.
output[attribute_name] = str(attribute)
---
2014-11-27 11:09:58 +03:00
2013-05-05 19:48:12 +04:00
# Advanced serializer usage
2014-11-28 12:56:44 +03:00
## Overriding serialization and deserialization behavior
2014-11-27 11:09:58 +03:00
2014-11-28 12:56:44 +03:00
If you need to alter the serialization, deserialization or validation of a serializer class you can do so by overriding the `.to_representation()` or `.to_internal_value()` methods.
2013-05-05 19:48:12 +04:00
2014-11-28 12:56:44 +03:00
Some reasons this might be useful include...
* Adding new behavior for new serializer base classes.
* Modifying the behavior slightly for an existing class.
* Improving serialization performance for a frequently accessed API endpoint that returns lots of data.
The signatures for these methods are as follows:
#### `.to_representation(self, obj)`
Takes the object instance that requires serialization, and should return a primitive representation. Typically this means returning a structure of built-in Python datatypes. The exact types that can be handled will depend on the render classes you have configured for your API.
#### ``.to_internal_value(self, data)``
Takes the unvalidated incoming data as input and should return the validated data that will be made available as `serializer.validated_data`. The return value will also be passed to the `.create()` or `.update()` methods if `.save()` is called on the serializer class.
If any of the validation fails, then the method should raise a `serializers.ValidationError(errors)`. Typically the `errors` argument here will be a dictionary mapping field names to error messages.
The `data` argument passed to this method will normally be the value of `request.data`, so the datatype it provides will depend on the parser classes you have configured for your API.
2013-08-07 22:00:06 +04:00
## Dynamically modifying fields
Once a serializer has been initialized, the dictionary of fields that are set on the serializer may be accessed using the `.fields` attribute. Accessing and modifying this attribute allows you to dynamically modify the serializer.
2013-07-04 16:49:28 +04:00
Modifying the `fields` argument directly allows you to do interesting things such as changing the arguments on serializer fields at runtime, rather than at the point of declaring the serializer.
### Example
For example, if you wanted to be able to set which fields should be used by a serializer at the point of initializing it, you could create a serializer class like so:
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.
"""
def __init__(self, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
fields = kwargs.pop('fields', None)
2013-08-07 22:00:06 +04:00
# Instantiate the superclass normally
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
2014-11-27 11:09:58 +03:00
if fields is not None:
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)
This would then allow you to do the following:
>>> class UserSerializer(DynamicFieldsModelSerializer):
>>> class Meta:
>>> model = User
>>> fields = ('id', 'username', 'email')
>>>
>>> print UserSerializer(user)
{'id': 2, 'username': 'jonwatts', 'email': 'jon@example.com'}
>>>
>>> print UserSerializer(user, fields=('id', 'email'))
{'id': 2, 'email': 'jon@example.com'}
2014-11-27 11:09:58 +03:00
## Customizing the default fields
2014-11-28 12:56:44 +03:00
REST framework 2 provided an API to allow developers to override how a `ModelSerializer` class would automatically generate the default set of fields.
2012-10-29 00:43:43 +04:00
2014-11-28 12:56:44 +03:00
This API included the `.get_field()`, `.get_pk_field()` and other methods.
2012-10-29 00:43:43 +04:00
2014-11-28 12:56:44 +03:00
Because the serializers have been fundamentally redesigned with 3.0 this API no longer exists. You can still modify the fields that get created but you'll need to refer to the source code, and be aware that if the changes you make are against private bits of API then they may be subject to change.
2012-10-29 00:43:43 +04:00
2014-11-28 12:56:44 +03:00
A new interface for controlling this behavior is currently planned for REST framework 3.1.
2012-09-01 23:26:27 +04:00
---
# Third party packages
The following third party packages are also available.
## MongoengineModelSerializer
2012-09-01 23:26:27 +04:00
The [django-rest-framework-mongoengine][mongoengine] package provides a `MongoEngineModelSerializer` serializer class that supports using MongoDB as the storage layer for Django REST framework.
2012-09-01 23:26:27 +04:00
## GeoFeatureModelSerializer
The [django-rest-framework-gis][django-rest-framework-gis] package provides a `GeoFeatureModelSerializer` serializer class that supports GeoJSON both for read and write operations.
## HStoreSerializer
The [django-rest-framework-hstore][django-rest-framework-hstore] package provides an `HStoreSerializer` to support [django-hstore][django-hstore] `DictionaryField` model field and its `schema-mode` feature.
2012-09-01 23:26:27 +04:00
[cite]: https://groups.google.com/d/topic/django-users/sVFaOfQi4wY/discussion
2013-02-24 02:01:29 +04:00
[relations]: relations.md
[mongoengine]: https://github.com/umutbozkurt/django-rest-framework-mongoengine
[django-rest-framework-gis]: https://github.com/djangonauts/django-rest-framework-gis
[django-rest-framework-hstore]: https://github.com/djangonauts/django-rest-framework-hstore
[django-hstore]: https://github.com/djangonauts/django-hstore