我的个人资料序列化器数据(而不是实际数据)为空

时间:2019-11-23 21:26:03

标签: python django django-rest-framework django-rest-auth

我正在使用Django-rest-auth来验证我的用户,效果很好。我的模型的设置方式是,我具有用于身份验证的自定义用户模型,而且还具有一个概要文件模型,该模型在创建用户时都会通过信号创建。

我希望在从用户的URL中提取用户时,还显示该用户的配置文件,并且已经通过了序列化程序。

问题:我得到的是空值而不是实际数据

我的models.py(我没有包括一些模型,例如用户管理器,技能等,因为我觉得它们并不相关)

class User(AbstractBaseUser, PermissionsMixin):
    username = None
    email = models.EmailField(max_length=254, unique=True)
    fullname = models.CharField(max_length=250)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    last_login = models.DateTimeField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['fullname']

    objects = UserManager()


class Profile(models.Model):

    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='profiles')
    date_of_birth = models.DateField(blank=True, verbose_name="DOB", null=True)
    bio = models.TextField(max_length=500, blank=True, null=True)
    profile_photo = models.CharField(blank=True, max_length=300, null=True)
    skills = models.ManyToManyField(Skill, related_name='skills')
    sex = models.CharField(max_length=1, choices=SEX, blank=True, null=True)
    type_of_body = models.CharField(max_length=8, choices=BODYTYPE, blank=True, null=True)
    feet = models.PositiveIntegerField(blank=True, null=True)
    inches = models.PositiveIntegerField(blank=True, null=True)
    lives_in = models.CharField(max_length=50, blank=True, null=True)
    updated_on = models.DateTimeField(auto_now_add=True)

serializers.py代码

class ProfileSerializer(serializers.ModelSerializer):

    class Meta:
        model = Profile
        fields = "__all__"

        read_only_fields = ('pk',)


class CustomUserDetailsSerializer(serializers.ModelSerializer):

    profiles = ProfileSerializer(read_only=True)

    class Meta:
        model = User
        fields = ('pk', 'email', 'fullname', 'profiles')
        read_only_fields = ('email', 'fullname', 'profiles')

view.py

class ListUsersView(APIView):

    permission_classes = [AllowAny]

    def get(self, request):
        user = User.objects.all()
        serializer = CustomUserDetailsSerializer(user, many=True)
        return Response(serializer.data)

urls.py

url(r'^list-users/$', ListUsersView.as_view(), name='list-users'),

我得到的JSON响应

[
    {
        "pk": 1,
        "email": "opeyemiodedeyi@gmail.com",
        "fullname": "opeyemi odedeyi",
        "profiles": {
            "date_of_birth": null,
            "bio": null,
            "profile_photo": null,
            "sex": null,
            "type_of_body": null,
            "feet": null,
            "inches": null,
            "lives_in": null
        }
    }
]

如何使个人资料显示在响应中?

1 个答案:

答案 0 :(得分:1)

我想问题出在您的CustomUserDetailsSerializer中。您与“用户”和“个人资料”具有一对多关系,但是您并未在序列化程序的profiles属性中明确地告诉它。您必须将many=True参数传递给ProfileSerializer

class CustomUserDetailsSerializer(serializers.ModelSerializer):

    profiles = ProfileSerializer(many=True, read_only=True)

    class Meta:
        model = User
        fields = ('pk', 'email', 'fullname', 'profiles')
        read_only_fields = ('email', 'fullname', 'profiles')

但是我很好奇为什么您要使用一对多关系。您可以使用OneToOneField明确告诉一个用户只能拥有一个配置文件。但是我不熟悉您的情况,所以这只是我的建议。