serializers.py 788 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from django.contrib.auth.models import User
from rest_framework import serializers
from student.models import UserProfile
from user_api.models import UserPreference


class UserSerializer(serializers.HyperlinkedModelSerializer):
    name = serializers.SerializerMethodField("get_name")

    def get_name(self, user):
        profile = UserProfile.objects.get(user=user)
        return profile.name

    class Meta:
        model = User
        # This list is the minimal set required by the notification service
17 18
        fields = ("id", "email", "name", "username")
        read_only_fields = ("id", "email", "username")
19 20 21 22 23 24 25 26


class UserPreferenceSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer()

    class Meta:
        model = UserPreference
        depth = 1