简体   繁体   English

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

[英]Attribute Error at API - module object has no attribute 'UserViewSet' - Django Rest Framework

Setting up Django Rest Framework - 设置Django Rest框架 -

Using the Quickstart tutorial from http://www.django-rest-framework.org/tutorial/quickstart/ 使用http://www.django-rest-framework.org/tutorial/quickstart/上的快速入门教程

Users and groups links work as expected. 用户和组链接按预期工作。 I got to add another link - following the same tutorial using appropriate naming conventions for my app and model and I get the error listed in the Title. 我必须添加另一个链接 - 使用适用于我的应用和模型的命名约定的相同教程,我得到标题中列出的错误。

I can comment out users and groups and the employees model will work. 我可以注释掉用户和组,员工模型也可以运行。 I can't get more than one link in the Django Rest framework to work properly. 我无法在Django Rest框架中获得多个链接以正常工作。 Any ideas? 有任何想法吗? Here is the urls code 这是网址代码

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'))
]

I see that you have an ambiguity in your imports. 我看到你的进口含糊不清。 Can you import the name explicitly: from XXXXXXaccess.views import UserViewSet ? 您可以显式导入名称:从XXXXXXaccess.views导入UserViewSet吗? Otherwise you will overwrite views with the definition of views from employees, where there is no UserViewSet defined, like the error says. 否则,您将使用员工的视图定义覆盖视图,其中没有定义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'))
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM