API的属性错误 - 模块对象没有属性' UserViewSet' - Django Rest Framework

时间:2015-10-13 14:37:11

标签: python api rest django-rest-framework django-1.8

设置Django Rest框架 -

使用http://www.django-rest-framework.org/tutorial/quickstart/

中的快速入门教程

用户和组链接按预期工作。我必须添加另一个链接 - 使用适用于我的应用和模型的命名约定的相同教程,我得到标题中列出的错误。

我可以注释掉用户和群组,员工模型也可以运作。我无法在Django Rest框架中获得多个链接以正常工作。有任何想法吗?这是网址代码

from django.conf.urls import url, include
from rest_framework import routers
from XXXXXXaccess import views
from employees import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
router.register(r'employees', views.EmployeeViewSet)


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

1 个答案:

答案 0 :(得分:2)

我发现你的进口含糊不清。您可以显式导入名称:从XXXXXXaccess.views导入UserViewSet吗?否则,您将使用员工的视图定义覆盖视图,其中没有定义UserViewSet,如错误所示。

from django.conf.urls import url, include
from rest_framework import routers
from XXXXXXaccess.views import UserViewSet, GroupViewSet
from employees.views import EmployeeViewSet

router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)
router.register(r'employees', EmployeeViewSet)


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