具有多个视图的Django Rest Framework路由器 [英] Django Rest Framework Routers with multiple viewsets

查看:159
本文介绍了具有多个视图的Django Rest Framework路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DRF 3.1创建一个API。我首先使用ModelViewSet创建管理功能,并使用DefaultRouter注册这些功能。这样做完美,默认的API根视图在浏览器中查看时生成了相应的链接。然后,我使用ReadOnlyModelViewSet为标准用户创建了更多受限的视图,当我尝试将这些受限制的视图集注册到路由器时出现问题,因为ReadOnlyModelViewSets似乎覆盖了API根视图上的链接。生成的网址在输入浏览器时似乎工作,但API根视图上的链接不符合我的预期。



serializers.py

  class AdminUnitSerializer(serializers.HyperlinkedModelSerializer):
'''
包含所有字段的管理用户的模型序列化程序

''

url = HyperlinkedIdentityField(view_name ='unit-detail')

class Meta:
model = Unit
fields =(' url','id','name','symbol','converters_from','converters_to')

class UserUnitSerializer(serializers.HyperlinkedModelSerializer):
'''
包含限制字段的标准用户的模型序列化程序

''

url = HyperlinkedIdentityField(view_name ='unit-detail')

类Meta:
model = Unit
fields =('url','id','name','symbol')

views.py

  class AdminUnitViewSet(viewsets.ModelViewSet):
'''
在单元对象上提供CRUD操作的简单视图

'''

queryset = models.Unit.objects.all()
serializer_class = serializers.AdminUnitSerializer
permission_classes =(permissions.IsAdminUser,)

class UserUnitViewSet(viewsets .ReadOnlyModelViewSet):
'''
只读视图集提供单元对象的列表和详细视图

''

queryset = models。 Unit.objects.all()
serializer_class = serializers.UserUnitSerializer
permission_classes(permissions.IsAuthenticated,)

urls.py

  router.register(r'manangeunits',views.AdminUnitViewSet)
router.register(r'readunits',views.UserUnitViewSet)

urlpatterns = [
url(r'^ api-auth /',include('rest_framework.urls',namespace =' rest_framework')),
url(r'^ rest-auth /',include(' rest_auth.urls')),
url(r'^',include(router.urls))
]

这样会产生一个这样的默认的API根视图

  {
manangeunits :http:// localhost:8000 / readunits /,
readunits:http:// localhost:8000 / readunits /,
}

而我正在寻找的是这个

  {
manangeunits:htt:// localhost:8000 / manageunits /,
readunits:htt:// localhost:8000 / readunits /,
}

url'htt:// localhost:8000 / manageunits /'在地址为键入浏览器,它不会出现在API根视图上



任何帮助将不胜感激

解决方案

我有同样的问题,幸运地找到了一个解决方案。



扩展你的urls.py如下:

  router.register(r'manangeunits',views.AdminUnit ViewSet,base_name ='manangeunits')
router.register(r'readunits',views.UserUnitViewSet,base_name ='readunits')

我不知道这背后的魔法,但它适用于我:)


I am attempting to create an API using DRF 3.1. I started by creating manangement functions using ModelViewSet and registering these with a DefaultRouter. This worked perfectly and the default API root view generated the appropriate links when viewed in a browser. I then created more restricted viewsets for standard users using ReadOnlyModelViewSet, the problem occurred when I tried to register these restricted viewsets with the router as the ReadOnlyModelViewSets appear to overwrite the links on the API root view. The generated urls appear to work when typed into the browser but the links on the API root view are not as I expected

serializers.py

class AdminUnitSerializer(serializers.HyperlinkedModelSerializer):
'''
model serializer for admin users containing all fields

'''

url = HyperlinkedIdentityField(view_name='unit-detail')

class Meta:
    model = Unit
    fields = ('url', 'id', 'name', 'symbol', 'converters_from', 'converters_to')

class UserUnitSerializer(serializers.HyperlinkedModelSerializer):
'''
model serializer for standard users containing restricted set of fields

'''

url = HyperlinkedIdentityField(view_name='unit-detail')

class Meta:
    model = Unit
    fields = ('url', 'id', 'name', 'symbol')

views.py

class AdminUnitViewSet(viewsets.ModelViewSet):
'''
simple viewset providing CRUD operations on unit objects

'''

queryset = models.Unit.objects.all()
serializer_class = serializers.AdminUnitSerializer
permission_classes = (permissions.IsAdminUser,)

class UserUnitViewSet(viewsets.ReadOnlyModelViewSet):
'''
read only view set providing list and detail views for unit objects

'''

queryset =  models.Unit.objects.all()
serializer_class = serializers.UserUnitSerializer
permission_classes(permissions.IsAuthenticated,)

urls.py

router.register(r'manangeunits', views.AdminUnitViewSet)
router.register(r'readunits', views.UserUnitViewSet)

urlpatterns = [
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^', include(router.urls))
]

this produces a default API root view like this

{
"manangeunits": "http://localhost:8000/readunits/",
"readunits": "http://localhost:8000/readunits/",
}

while what I am looking for is this

{
"manangeunits": "htt://localhost:8000/manageunits/",
"readunits": "htt://localhost:8000/readunits/",
}

the url 'htt://localhost:8000/manageunits/' is valid and accessible when the address is typed into the browser it just does not appear on the API root view

any help would be greatly appreciated

解决方案

I had the same issue and luckily found a solution.

Extend your urls.py as following:

router.register(r'manangeunits', views.AdminUnitViewSet, base_name='manangeunits')
router.register(r'readunits', views.UserUnitViewSet, base_name='readunits')

I don't know the magic behind this, but it works for me :)

这篇关于具有多个视图的Django Rest Framework路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆