title: Django REST Framework -- Serializing many-to-many table relationships
body: <p>I'm creating an API for a website for which I have a table with Books, another with Hashtags (which basically is a catalog of hashtags, e.g. "#traveling" ) and an intermediate table to establish many-to-many relationships between Books and Hashtags. My models are:</p>
  
  <pre><code># models.py
  class Books(models.Model):
      id_books = models.AutoField(primary_key=True)
      title = models.CharField(max_length=150)
  
  class Hashtags(models.Model):
      id_hashtags = models.AutoField(primary_key=True)
      hashtag = models.CharField(max_length=150)
  
  class Books_Hashtags(models.Model):
      id_books_hashtags = models.AutoField(primary_key=True)
      id_books = models.ForeignKey(Books, on_delete=models.CASCADE)
      id_hashtags = models.ForeignKey(Hashtags, on_delete=models.CASCADE)
      class Meta:
          unique_together = ("id_books", "id_hashtags")
  </code></pre>
  
  <p>I'd like to have an API that provides an output like this:</p>
  
  <pre><code>[
      {
          "id_books": 1,
          "title": "The Hobbit",
          "hashtags": [
              {
                  "id_hashtags": 1,
                  "hashtag": "fantasy"
              },
              {
                  "id_hashtags": 2,
                  "hashtag": "fiction"
              },
              {
                  "id_hashtags": 3,
                  "hashtag": "middle earth"
              },
          ]
      }
  ]    
  </code></pre>
  
  <p>For this purpose I've serialized the models above using DRF following some examples that I've found online:</p>
  
  <pre><code>#serializers.py
  from rest_framework import serializers
  from app.models import Books, Books_Hashtags, Hashtags
  
  class HashtagsSerializer(serializers.ModelSerializer):
      hashtags = serializers.CharField()
      class Meta:
          model = Hashtags
          fields = ('hashtag',)
  
  class Books_HashtagsSerializer(serializers.ModelSerializer):
      id_hashtag = HashtagsSerializer(many=True, read_only=True)
      class Meta:
          model = Books_Hashtags
          fields = ('id_hashtags',)
  
  class BooksSerializer(serializers.ModelSerializer):
      id_books = serializers.IntegerField()
      id_books_hashtags = Books_HashtagsSerializer(many=True, read_only=True)
      title = serializers.CharField()
  
      class Meta:
          model = Books
          fields = ('id_books', 'title', 'id_books_hashtags',)
  </code></pre>
  
  <p>My views look like this:</p>
  
  <pre><code>#views.py
  from ntgBackend.models import Books
  from rest_framework import viewsets
  from api.serializers import BooksSerializer
  
  class BooksViewSet(viewsets.ModelViewSet):
      serializer_class = BooksSerializer
      queryset = Books.objects.all()
  </code></pre>
  
  <p>So far, I've managed to obtain the following output from the API:</p>
  
  <pre><code>[
      {
          "id_books": 1,
          "title": "The Hobbit",
      }
  ]
  </code></pre>
  
  <p>But no information regarding hashtags, which I know they exist in the DB.
  I'm using DRF 3.8.2 with a MySQL DB and Django 2.0.2.
  Please help!</p>