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”对象不可迭代
1条答案
按热度按时间vof42yt11#
尝试
字符串