title: How can I apply a filter to a nested resource in Django REST framework?
body: <p>In my app I have the following models:</p>
  
  <pre><code>class Zone(models.Model):
      name = models.SlugField()
  
  class ZonePermission(models.Model):
      zone = models.ForeignKey('Zone')
      user = models.ForeignKey(User)
      is_administrator = models.BooleanField()
      is_active = models.BooleanField()
  </code></pre>
  
  <p>I am using Django REST framework to create a resource that returns zone details plus a nested resource showing the authenticated user's permissions for that zone.  The output should be something like this:</p>
  
  <pre><code>{
      "name": "test", 
      "current_user_zone_permission": {
          "is_administrator": true, 
          "is_active": true
      }
  } 
  </code></pre>
  
  <p>I've created serializers like so:</p>
  
  <pre><code>class ZonePermissionSerializer(serializers.ModelSerializer):
      class Meta:
          model = ZonePermission
          fields = ('is_administrator', 'is_active')
  
  class ZoneSerializer(serializers.HyperlinkedModelSerializer):
      current_user_zone_permission = ZonePermissionSerializer(source='zonepermission_set')
  
      class Meta:
          model = Zone
          fields = ('name', 'current_user_zone_permission')
  </code></pre>
  
  <p>The problem with this is that when I request a particular zone, the nested resource returns the ZonePermission records for <strong>all</strong> the users with permissions for that zone.  Is there any way of applying a filter on <code>request.user</code> to the nested resource?</p>
  
  <p>BTW I don't want to use a <code>HyperlinkedIdentityField</code> for this (to minimise http requests).</p>
  
  <h1>Solution</h1>
  
  <p>This is the solution I implemented based on the answer below.  I added the following code to my serializer class:</p>
  
  <pre><code>current_user_zone_permission = serializers.SerializerMethodField('get_user_zone_permission')
  
  def get_user_zone_permission(self, obj):
      user = self.context['request'].user
      zone_permission = ZonePermission.objects.get(zone=obj, user=user)
      serializer = ZonePermissionSerializer(zone_permission)
      return serializer.data
  </code></pre>
  
  <p>Thanks very much for the solution!</p>