如何在Django中从select_related查询序列化结果

时间:2019-03-28 10:46:37

标签: django python-3.x serialization

我想使用Django select_related连接两个表。但是我该如何序列化来自两个表的那些数据以获得序列化的详细信息,包括来自另一个表的完整图像url

我的View.py

class getALlUserProfileDataAPIView(views.APIView):
    permission_classes = (permissions.AllowAny,)

    def get(self, request):

        #usedraa = User.objects.filter(qs).select_related('userAddress').values('id','useraddress__country',  'useraddress__state',  'useraddress__city','email','username','first_name','last_name','password')

        usedraa = User.objects.filter().select_related('Profile').values('username','profile__image','first_name','last_name')
        #usedraa = User.objects.all().prefetch_related()
        userProfileSerializer0 = userProfileSerializer(usedraa, context={'request': request}, many=True)

        return Response(userProfileSerializer0.data, status=status.HTTP_201_CREATED)

跟踪器

ass userProfileSerializer(serializers.ModelSerializer):
    prodata = serializers.HyperlinkedIdentityField(read_only=True,  view_name = 'profile')
  #  prodata = ProfileSerializer(read_only=True, many=True)  # many=True is required
    class Meta:
        model = User
        fields = ("first_name","last_name","username","prodata")

0 个答案:

没有答案