码迷,mamicode.com
首页 > 其他好文 > 详细

rest_framework_HyperlinkedIdentityField

时间:2018-08-03 01:14:00      阅读:119      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!