title: ModelSerializer is extremely slow in Django REST framework
body: <p>I am using Django REST framework for my API and yesterday I wanted to see how it works for large data. I found <a href="http://www.dabapps.com/blog/api-performance-profiling-django-rest-framework/" rel="noreferrer">this tutorial</a> about how to profile your requests (written by Tom Christie) and I discovered that for 10.000 users, my request was taking an astonishing 2:20 minutes.</p>
  
  <p>Most of the time was being spent on serializing the objects (around 65%) so I was wondering what can I do to speed things up ?</p>
  
  <p>My user model is actually extending the default django model, so using <a href="https://docs.djangoproject.com/en/stable/ref/models/querysets/#values" rel="noreferrer">.values()</a> does not work, because I am not also getting the nested model (even though it is a LOT faster).</p>
  
  <p>Any help would be greatly appreciated :)</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I am already using .select_related() when retrieving my queryset, and it has improved my time, but only by a few seconds. The number of total queries is 10, so my problem is not with the database access.</p>
  
  <p>Also, I am using .defer(), in order to avoid fields that I don't need in this request. That also provided a small improvement, but not enough.</p>
  
  <p><strong>Edit #2</strong></p>
  
  <p><code>Models</code></p>
  
  <pre><code>from django.contrib.auth.models import User
  from django.db.models import OneToOneField
  from django.db.models import ForeignKey
  
  from userena.models import UserenaLanguageBaseProfile
  from django_extensions.db.fields import CreationDateTimeField
  from django_extensions.db.fields import ModificationDateTimeField
  
  from mycompany.models import MyCompany
  
  
  class UserProfile(UserenaLanguageBaseProfile):
      user = OneToOneField(User, related_name='user_profile')
      company = ForeignKey(MyCompany)
      created = CreationDateTimeField(_('created'))
      modified = ModificationDateTimeField(_('modified'))
  </code></pre>
  
  <p><code>Serializers</code></p>
  
  <pre><code>from django.contrib.auth.models import User
  
  from rest_framework import serializers
  
  from accounts.models import UserProfile
  
  
  class UserSerializer(serializers.ModelSerializer):
      last_login = serializers.ReadOnlyField()
      date_joined = serializers.ReadOnlyField()
      is_active = serializers.ReadOnlyField()
  
      class Meta:
          model = User
          fields = (
              'id',
              'last_login',
              'username',
              'first_name',
              'last_name',
              'email',
              'is_active',
              'date_joined',
          )
  
  
  class UserProfileSerializer(serializers.ModelSerializer):
      user = UserSerializer()
  
      class Meta:
          model = UserProfile
          fields = (
              'id',
              'user',
              'mugshot',
              'language',
          )
  </code></pre>
  
  <p><code>Views</code></p>
  
  <pre><code>class UserProfileList(generics.GenericAPIView,
                        mixins.ListModelMixin,
                        mixins.CreateModelMixin):
  
      serializer_class = UserProfileSerializer
      permission_classes = (UserPermissions, )
  
      def get_queryset(self):
          company = self.request.user.user_profile.company
          return UserProfile.objects.select_related().filter(company=company)
  
      @etag(etag_func=UserListKeyConstructor())
      def get(self, request, *args, **kwargs):
          return self.list(request, *args, **kwargs)
  </code></pre>