Django serializer classes referencing each other

By bayman

I need to have two serializer classes referencing each other but I’m getting one of the classes not defined if both classes are referencing each other. I created a duplicate of one of the class w/ a difference name and this seems to work but is there a better way to do this without making 3 classes?

class ArtistSerializer(serializers.ModelSerializer):
    name = serializers.CharField()
    class Meta:
        model = Artist
        fields = ('id', 'name',)
class TrackSerializer(serializers.ModelSerializer):
    artist = ArtistSerializer(read_only=True)
    class Meta:
        model = Track
        fields = ('id', 'artist', 'title',)
class ArtistSerializer2(serializers.ModelSerializer):
    name = serializers.CharField()
    tracks = TrackSerializer(many=True, read_only=True)
    class Meta:
        model = Artist
        fields = ('id', 'name', 'slug', 'tracks')

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273