I'm using django 1.8 with djangorestframework==3.6.3. I took the model examples from the documentation:
https://www.django-rest-framework.org/api-guide/relations/#stringrelatedfield
Here are my serializers:
from rest_framework import serializers
from .models import *
class AlbumSerializer(serializers.ModelSerializer):
tracks = serializers.StringRelatedField(many=True)
class Meta:
model = Album
fields = ('album_name', 'artist', 'tracks')
class TrackSerializer(serializers.ModelSerializer):
class Meta:
model = Track
fields = ('order', 'title', 'duration', 'album')
and here is how I call the serializer:
def index(request):
if Track.objects.all().count() == 0:
album = Album.objects.create(album_name='something', artist='John')
Track.objects.create(album=album, order=1, title='something', duration=1)
print TrackSerializer(instance=Track.objects.all()[0]).data
return render(request, 'index.html')
The print statement give me: {'duration': 1, 'album': 1, 'order': 1, 'title': u'something'} Why is it not giving me all the field data for the corresponding album?