2👍
✅
You can add to serializer two field user_id
and user_name
. And specify write_only
and read_only
arguments to use user_id
only for POST or PUT and user_name
for GET requests:
class MemberSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(write_only=True)
user_name = serializers.CharField(source='user.username', read_only=True)
class Meta:
fields = ('user', 'user_name')
Source:stackexchange.com