django “CustomUser”对象不可迭代

6tr1vspr  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(108)

serializers.py(示例网站)

class UserprofileSerializer(ModelSerializer):
    class Meta:
        model = Profile
        fields = ['id','mobile','pincode', 'address','profileimage','coverImg']

class UserSerializer(ModelSerializer):
    profile = UserprofileSerializer(required=True,)
    class Meta:
        model = CustomUser
        fields = ['email', 'first_name', 'created_at','profile']

字符串

views.py(示例网站)

class profiledata(mixins.RetrieveModelMixin,
                      mixins.ListModelMixin,
                      generics.GenericAPIView):
    serializer_class = UserSerializer
    lookup_field = 'id'
    authentication_classes = [
        JWTAuthentication, TokenAuthentication, SessionAuthentication, BasicAuthentication]
    permission_classes = [IsAuthenticated, ]

    def get_queryset(self):
        user = CustomUser.objects.get(email=self.request.user)
        return user

    def get(self, request, id=None):
       if id:
            return self.retrieve(request)
       else:
            item = self.list(request)
            return item


当我尝试获取单个数据并将其序列化时,此时生成的错误“CustomUser”对象不可迭代

vof42yt1

vof42yt11#

尝试

....
    def get_queryset(self):
        return CustomUser.objects.filter(email=self.request.user)

    def get(self, request, id=None):
        if id:
            return self.retrieve(request)
        else:
            return self.list(request)

字符串

相关问题