标签:mod als rest request text print http col false
#生成链接 HyperlinkedIdentityField class UserInfoSerializer(serializers.ModelSerializer): group = serializer.HyperlinkedIdentityField(view_name=‘gp‘, lookup_field=‘group_id‘),lookup_url_kwarg=‘xxx‘ class Meta: model = models.UserInfo fields = [‘id‘,‘group‘] depth = 0 class UserInfoView(APIView): def get(self, request, *args, **kwargs): users = models.UserInfo.objects.all() ser = UserInfoSerializer(instance=users, many=True, context={‘request‘, request}) ret = json.dumps(ser.data, ensure_ascii=False) return HttpResponse(ret) urls.py urlpatterns = [ url(r‘^(?P<version>[v1|v2]+)/group(?P<xxx>\d+)$‘, views.GroupView.as_view(), name=‘gp‘), ] class GroupView(APIView): def get(self, request, *args, **kwargs): pk = kwargs.get(‘xxx‘) print(pk) obj = models.UserGroup.objects.filter(pk=pk).first() ser = GroupSerializer(instance=obj, many=False) ret = json.dumps(ser.data, ensure_ascii=False) return HttpResponse(ret)
rest_framework_HyperlinkedIdentityField
标签:mod als rest request text print http col false
原文地址:https://www.cnblogs.com/Liang-jc/p/9410723.html