[Answered ]-DRF using prefetch_related

1๐Ÿ‘

โœ…

I thought prefretch_related does that trick but in DRF.

This will fetch the Components for the Vessels, but since your serializers do not serialize these components, these will not end up in the result.

You should define the ComponentSerializer as subserializer for VesselSerializer, so:

class ComponentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Component
        fields = '__all__'

class VesselSerializer(serializers.ModelSerializer):
    vessel_components = ComponentSerializer(many=True)  # ๐Ÿ–˜ subserializer
    
    class Meta:
        model = Vessel
        fields = '__all__'

Leave a comment