Django REST Framework: not displayed fields of the related objects?


Faced with the fact that when you serialize a simple object, not shown linked objects (or rather fields of the associated object,except for ID) as indicated in the tutorial, Serializer relations
In fact, all of the code from there
class Album(models.Model):
 album_name = models.CharField(max_length=100)
 artist = models.CharField(max_length=100)

class Track(models.Model):
 album = models.ForeignKey(Album, related_name='tracks', on_delete=models.CASCADE)
 order = models.IntegerField()
 title = models.CharField(max_length=100)
 duration = models.IntegerField()

 class Meta:
 unique_together = ('album', 'order')
 ordering = ['order']

 def __str__(self):
 return '%d: %s' % (self.order, self.title)
class AlbumSerializer(serializers.ModelSerializer):
 tracks = serializers.StringRelatedField(many=True)

 class Meta:
 model = Album
 fields = ('album_name', 'artist', 'tracks')
def album_list(request):
 if request.method == 'GET':
 albums = Album.objects.all().prefetch_related('tracks')
 album_serializer = AlbumSerializer(albums, many=True)
 return Response(

The output JSON get just a Track with ID:
{"data":[{"type":"album_list","id":"1","attributes":{"album_name":"Ride the Lightning","artist":"Metallica"},"relationships":{"tracks":{"data":[{"type":"Track","id":"1"},{"type":"Track","id":"2"},{"type":"Track","id":"3"}],"meta":{"count":3}}}},{"type":"album_list","id":"2","attributes":{"album_name":"Metallica","artist":"Vetallica"},"relationships":{"tracks":{"data":[],"meta":{"count":0}}}}]}

Waiting-as in the example to something like
'tracks': [
'1: Sunflower',
'2: Whitetail',
'3: Dinosaur Act',

Shoveled the entire Internet, was tested in several test projects went down to this simple example is not working. Various methods of communications too tried. Where to dig?
March 19th 20 at 08:42
2 answers
March 19th 20 at 08:44
The answer himself.
The problem found here in this rendering.
They manage to brew such a mess that the child elements are returned. Killed 1.5 days on this stuff.
they need to be removed or replaced? - Cameron_Skiles commented on March 19th 20 at 08:47
March 19th 20 at 08:46
class TrackSerializer(serializers.ModelSerializer):
 class Meta:
 model = Track
 fields = '__all__'

class AlbumSerializer(serializers.ModelSerializer):
 tracks =TrackSerializer(read_only=True, many=True)

 class Meta:
 model = Album
 fields = ('album_name', 'artist', 'tracks')

Find more questions by tags Django