Skip to content

serializers

MyTokenObtainPairSerializer

Bases: TokenObtainPairSerializer

Source code in users/serializers.py
18
19
20
21
22
23
24
25
26
27
28
29
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)
        # Add custom claims
        token['username'] = user.username
        token['email'] = user.email
        token['name'] = f'{user.first_name} {user.last_name}' if user.first_name else user.username
        token['group'] = user.groups.first().name if user.groups.first() else 'Researcher'
        token['user_id'] = user.id

        return token

get_token(user) classmethod

Source code in users/serializers.py
19
20
21
22
23
24
25
26
27
28
29
@classmethod
def get_token(cls, user):
    token = super().get_token(user)
    # Add custom claims
    token['username'] = user.username
    token['email'] = user.email
    token['name'] = f'{user.first_name} {user.last_name}' if user.first_name else user.username
    token['group'] = user.groups.first().name if user.groups.first() else 'Researcher'
    token['user_id'] = user.id

    return token

ShortUserSerializer

Bases: serializers.ModelSerializer

Source code in users/serializers.py
12
13
14
15
class ShortUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'id']

Meta

Source code in users/serializers.py
13
14
15
class Meta:
    model = User
    fields = ['username', 'id']

fields = ['username', 'id'] class-attribute

model = User class-attribute

UserSerializer

Bases: serializers.HyperlinkedModelSerializer

Source code in users/serializers.py
6
7
8
9
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'email']

Meta

Source code in users/serializers.py
7
8
9
class Meta:
    model = User
    fields = ['username', 'email']

fields = ['username', 'email'] class-attribute

model = User class-attribute