1

I'm stuck and I don't know what's wrong... to me it's identical to the "Nested relationships" example in the DRF API guide but something is not right...

MODEL

class PlayerSquadra(models.Model):
    
    player = models.ForeignKey(
       'app_player.Player',
       on_delete=models.CASCADE,
       verbose_name=_('giocatore'),
       related_name='player_squadraplayer',
    )

    squadra = models.ForeignKey(
        'app_stagione.Squadra',
        on_delete=models.CASCADE,
        verbose_name=_('squadra'),
        related_name='squadra_squadraplayer'
    )

    def __str__(self):
        return '%s' % (self.player)

URL

router.register(r'squadraJSON/(?P<squadra>.*)', views.SquadraViewSet)

VIEW

class SquadraViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Squadra.objects.all()
    serializer_class = SquadraSerializer

    def get_queryset(self):
        laSquadra = self.kwargs['squadra']
        queryset = Squadra.objects.filter(id=int(laSquadra))
        return queryset

SERIALIZER

class PlayerSquadraSerializer(serializers.ModelSerializer):
    class Meta:
        model = PlayerSquadra
        fields = '__all__'

class SquadraSerializer(serializers.ModelSerializer):
    playersquadra = PlayerSquadraSerializer(many=True, read_only=True)

    class Meta:
        model = Squadra
        fields = ['nomeSquadra','id','playersquadra']

What I get when I call http://192.168.0.102:8000/squadraJSON/26/ is:

GET /squadraJSON/26/
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "nomeSquadra": "prova2",
        "id": 26
    }
]

And no errors... While I expect something like this:

GET /squadraJSON/26/
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "nomeSquadra": "prova2",
        "id": 26
        "playersquadra": [
            {'id': 1, 'firstName': 'Michael', 'lastName': 'Jordan',...},
            {'id': 2, 'firstName': 'Larry', 'lastName': 'Bird',...},
            ...
        ],
    }
]

Could you give me some hint why I'm not getting all the players belonging to Squadra with id=26? Thanks for helping

1 Answer 1

2

You have to use the related_name to access all the related PlayerSquadra, which is squadra_squadraplayer, so:

class SquadraSerializer(serializers.ModelSerializer):
    playersquadra = PlayerSquadraSerializer(
        many=True, read_only=True, source='squadra_squadraplayer',
    )

    class Meta:
        model = Squadra
        fields = ['nomeSquadra','id','playersquadra']

or:

class SquadraSerializer(serializers.ModelSerializer):
    # This will change the name of the key to squadra_squadraplayer
    squadra_squadraplayer = PlayerSquadraSerializer(many=True, read_only=True)

    class Meta:
        model = Squadra
        fields = ['nomeSquadra','id','squadra_squadraplayer']

or even:

class SquadraSerializer(serializers.ModelSerializer):
    playersquadra = serializers.SerializerMethodField()

    class Meta:
        model = Squadra
        fields = ['nomeSquadra','id','playersquadra']
    
    def get_playersquadra(self, obj):
        return PlayerSquadraSerializer(
            obj.squadra_squadraplayer, many=True, read_only=True,
        ).data
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick answer... just a comment: I tried the first proposed solution and works but what about the example in DRF documentation? Is it just missing the source argument (so it could be useful to correct it) and it doesn't work as it is or am I missing something? Thanks again for helping!
It didn't need the source argument because the field name (tracks) matches the related_name of track from albums: album = models.ForeignKey(Album, related_name='tracks', on_delete=models.CASCADE)
This is also shown on my 2nd example. squadra_squadraplayer matches the related name so no need to define source. Let me know if it's not clear
Another example is if you want to use playersquadra in the serializer without specifying source, change the related name of squadra foreign key from squadra_squadraplayer to playersquadra in the PlayerSquadra model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.