[Solved]-Problem reusing serializers with django and drf-yasg

18👍

According to drf-yasg docs here, you can specify a ref_name in a Meta class of serializer. Therefore, something like this should work (although not very clean solution):

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = users_models.User
        fields = (users_models.User.first_name.field_name,
                  users_models.User.last_name.field_name,)


class FirstUserSerializer(UserSerializer):
    class Meta:
        ref_name = "User 1"


class SecondUserSerializer(UserSerializer):
    class Meta:
        ref_name = "User 2"


class FooRequestSerializer(serializers.ModelSerializer):
      first_user = FirstUserSerializer(help_text="first user")
      second_user = SecondUserSerializer(help_text="second user")

Leave a comment