I’m not sure if the title is actually what I want to do, or if it’s just the result of my ignorance on best practices, but here goes:
As an example, I’d like to return a list of names in JSON if someone visits root/users, but return more details if someone visits root/usersdetailed.
Would I have to make two different serializers for this, or is there some sort of logic I can throw into my serializer class to check what to change?
My serializer class looks similar to this
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username', 'userid', 'gender']
My janky solution:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username', 'userid', 'gender']
class UserSerializerForNamesOnly(serializers.ModelSerializer):
class Meta:
model = User
fields = ['username']