from rest_framework import serializers from .models import Member class MemberSerializer(serializers.ModelSerializer): username = serializers.SerializerMethodField() rolename = serializers.SerializerMethodField() @classmethod def get_username(cls, instance): user = instance.user return user.username if user else None @classmethod def get_rolename(cls, instance): role = instance.role return role.name if role else None class Meta: model = Member fields = ('id', 'user', 'role', 'username', 'rolename')