I have a problem when getting session information inside my serializer method field that I had
class PersonSerializer(serializers.ModelSerializer): role = serializers.SerializerMethodField() def get_role(self, obj): # I have to get session in here. pass class Meta: model = Person
First time we need to do is, get request object in our method, then we can get our session information on it.
def get_role(self, obj): # Get request object. request = self.context.get('request') # Get session information. session_information = request.session.get('my_session')