Search code examples
djangodjango-rest-frameworkjson-api

Is there a way to show more details about a Relational Field in Django REST Framework JSON API


I'm trying to use the Django REST Framework JSON API and was wondering if there is a way to serialize Relations (ForeignKey and all) with a bit more detail compared to :

"post": {
          "type": "Post",
          "id": 1
        }

I was thinking of something along the lines of :

"post": {
          "type": "Post",
          "id": 1,
          "name": "First Post"
          "tag": "Development"
        }

Solution

  • class AlbumSerializer(serializers.ModelSerializer):
        tracks = serializers.StringRelatedField(many=True)
    
        class Meta:
           model = Album
           fields = ('album_name', 'artist', 'tracks')
    

    for relations look here: http://www.django-rest-framework.org/api-guide/relations/