P
P
pistollo2019-03-26 19:21:22
Django
pistollo, 2019-03-26 19:21:22

Django REST Framework: Not displaying fields of related objects?

Hello!
Faced with the fact that when serializing a simple object, related objects are not displayed (more precisely, the fields of the related object, except for the ID), as indicated in the Serializer relations
tutorial Actually, all the code from there
is models.py

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)

serializers.py
class AlbumSerializer(serializers.ModelSerializer):
    tracks = serializers.StringRelatedField(many=True)

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

views.py
@api_view(['GET',])
@permission_classes((AllowAny,))
def album_list(request):
    if request.method == 'GET':
        albums = Album.objects.all().prefetch_related('tracks')
        album_serializer = AlbumSerializer(albums, many=True)
        return Response(album_serializer.data)

As a JSON output, I just get 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}}}}]}
Expectation - as in the example, get something like
'tracks': [
'1: Sunflower',
'2: Whitetail',
'3: Dinosaur Act',
...
I dug up the entire Internet, tried it in several test projects - I dropped down to this simple example - it does not work. I've also tried various connection methods. Where can you dig?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pistollo, 2019-03-26
@pistollo

I will answer myself.
I found the problem in these renders.
'DEFAULT_RENDERER_CLASSES': (
'rest_framework_json_api.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
They manage to make such a mess that child margins are not returned. Killed 1.5 days for this nonsense.

F
FulTupFul, 2019-03-26
@FulTupFul

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')

A
aglgl, 2020-05-06
@aglgl

'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.BrowsableAPIRenderer',
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.TemplateHTMLRenderer',
),
This helps in setting.py

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question