简体   繁体   中英

Django: Get User profile from User serializer

I have a basic ‍‍‍‍ UserProfile model that has a OneToOne relationship with a User model.

My User serializer works well, though how do I go about retrieving the related UserProfile model from the same serializer and viewset ?

Here is my UserProfile Model:

class UserProfile(models.Model):
    bio = models.CharField(max_length=1000)
    cellphone = PhoneNumberField(null=True)
    useraccount = models.OneToOneField(User, on_delete=models.CASCADE)

My current viewset:

class CurrentUserViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = CurrentUserSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        user = self.request.user
        queryset = User.objects.filter(username = user)
        return queryset

My User serializer:

class CurrentUserSerializer(serializers.ModelSerializer):
    groups = GroupSerializer(many=True)
    
    class Meta:
        model = User
        fields = ('username', 'email', 'first_name', 'last_name', 'groups')

    def to_representation(self, instance):

        return {
            'username': instance.username,
            'email': instance.email,
            'first_name': instance.first_name,
            'last_name': instance.last_name,
            'groups':  GroupSerializer(instance.groups.all(), many=True).data,
        }

I attempted the following with my Serializers, but got an attribute error stating that my User model does not have the field profile :

class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('dashboards', 'fullname')

class CurrentUserSerializer(serializers.ModelSerializer):
    groups = GroupSerializer(many=True)
    profile = TeamMemberDashboardSerializer()
    
    class Meta:
        model = User
        fields = ('username', 'email', 'first_name', 'last_name', 'groups', 'profile')

    def to_representation(self, instance):

        return {
            'username': instance.username,
            'email': instance.email,
            'first_name': instance.first_name,
            'last_name': instance.last_name,
            'groups':  GroupSerializer(instance.groups.all(), many=True).data,
            'profile': instance.profile
        }

Thanks for your time!

The easiest way is to put, for showing profile data is to put depth = 1 in class Meta of your serializer as follows:

class CurrentUserSerializer(serializers.ModelSerializer):
    groups = GroupSerializer(many=True)
    
    class Meta:
        model = User
        fields = ('username', 'email', 'first_name', 'last_name', 'groups')
        depth = 1 

    def to_representation(self, instance):

        return {
            'username': instance.username,
            'email': instance.email,
            'first_name': instance.first_name,
            'last_name': instance.last_name,
            'groups':  GroupSerializer(instance.groups.all(), many=True).data,
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM