Serializers

First up we're going to define some serializers. Let's create a new module named tutorial/quickstart/serializers.py that we'll use for our data representations.

  1. from django.contrib.auth.models import User, Group
  2. from rest_framework import serializers
  3. class UserSerializer(serializers.HyperlinkedModelSerializer):
  4. class Meta:
  5. model = User
  6. fields = ['url', 'username', 'email', 'groups']
  7. class GroupSerializer(serializers.HyperlinkedModelSerializer):
  8. class Meta:
  9. model = Group
  10. fields = ['url', 'name']

Notice that we're using hyperlinked relations in this case with HyperlinkedModelSerializer. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.