title: Django Rest DRF : accessing a foreign key reference from reverse relation
body: <p>Let's say I have two models.</p>
  
  <blockquote>
    <p>Model</p>
  </blockquote>
  
  <pre><code>class Item(models.Model):
      name = models.CharField(max_length=32)
      # other fields
  
  class ItemRelation(models.Model):
      item = models.ForeignKey(Item, related_name='relations_item')
      user = models.ForeignKey(User, related_name='relations_user')
      has_viewed = models.BooleanField(default=False)
      has_loved = models.BooleanFields(default=False)
  </code></pre>
  
  <p>Now, what I want to do is to get the profile of one user which would contain the items associated with that user having has_loved=True and has_viewed=True.</p>
  
  <p>In my views.py file I had something like this.</p>
  
  <pre><code>class UserProfile(APIView):
      def get(self, request, format=None):
          id = self.request.query_params.get('id')
          user = User.objects.filter(id=id).prefetch_related(Prefetch(
              'relations_user', queryset=ItemRelation.objects.select_related('item').filter(has_viewed=True),
               to_attr='item_viewed'
      ))
  </code></pre>
  
  <p>I was certain that I was wrong and I also got a serializer error, since I was trying to serialize an ItemRelation object using a serializer which used the Item as its model. </p>
  
  <blockquote>
    <p>EDIT</p>
  </blockquote>
  
  <p><strong>serializers.py</strong></p>
  
  <pre><code>class ItemSerializer(seralizer.ModelSerializer):
      class Meta:
          model = Item
          fields = ['name']
  
  class UserSerializer(serializers.ModelSerializer):
      relations_user = ItemSerializer(read_only=True, many=True)
      class Meta:
          model = User
          fields = ['username', 'relations_user']
  </code></pre>