django rest框架DefaultRouter错误

时间:2018-05-13 16:55:34

标签: django django-rest-framework

我写了一个学习django rest框架的程序,但我编写的程序没有正确执行。我一直在寻找它。我找不到错误。我需要有人帮助我从另一个角度检查问题。谢谢。

models.py

from django.db import models


class Subsystem(models.Model):
    name = models.CharField(max_length=36)


class Menu(models.Model):
    subsystem = models.ForeignKey(Subsystem, on_delete=models.CASCADE)
    name = models.CharField(max_length=36)

serializers.py

from rest_framework.serializers import ModelSerializer
from subsystem.models import Subsystem, Menu


class SubsystemSerializer(ModelSerializer):
    class Meta:
        model = Subsystem
        fields = ('name', )


class MenuSerializer(ModelSerializer):
    class Meta:
        model = Menu
        fields = ('name', 'subsystem')

views.py

from rest_framework.viewsets import ModelViewSet
from subsystem.models import Subsystem, Menu
from subsystem.api.serializers import SubsystemSerializer, MenuSerializer


class SubsystemViewSet(ModelViewSet):
    queryset = Subsystem.objects.all()
    serializer_class = SubsystemSerializer(queryset)


class MenuViewSet(ModelViewSet):
    queryset = Menu.objects.all()
    serializer_class = MenuSerializer

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from subsystem.api.views import SubsystemViewSet
router = DefaultRouter()

router.registry(r'subsystems', SubsystemViewSet)

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

我开始尝试运行python manage.py runserver但它无法运行

错误:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x06125FA8>
Traceback (most recent call last):
  File "D:\env\python\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "D:\env\python\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "D:\env\python\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\env\python\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "D:\env\python\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\env\python\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "D:\env\python\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "D:\env\python\lib\site-packages\django\urls\resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "D:\env\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\env\python\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\env\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\env\python\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "D:\env\python\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\sc\oa\oaapi\oaapi\urls.py", line 18, in <module>
    router.registry(r'subsystems', SubsystemViewSet)
TypeError: 'list' object is not callable

我修改了views.py

class SubsystemViewSet(ModelViewSet):
    queryset = Subsystem.objects.all()
    serializer_class = SubsystemSerializer

但错误仍然存​​在 细节:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x065F5FA8>
Traceback (most recent call last):
  File "D:\env\python\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "D:\env\python\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "D:\env\python\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\env\python\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "D:\env\python\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\env\python\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "D:\env\python\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "D:\env\python\lib\site-packages\django\urls\resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "D:\env\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\env\python\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\env\python\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\env\python\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "D:\env\python\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\sc\oa\oaapi\oaapi\urls.py", line 18, in <module>
    router.registry(r'subsystems', SubsystemViewSet)
TypeError: 'list' object is not callable

1 个答案:

答案 0 :(得分:1)

ViewSet&#39; serializer_class应该是类本身,而不是它的实例。您应该使用serializer_class = SubsystemSerializer代替serializer_class = SubsystemSerializer(queryset)。将SubsystemViewSet重写为:

class SubsystemViewSet(ModelViewSet):
    queryset = Subsystem.objects.all()
    serializer_class = SubsystemSerializer

它也应该是register而不是registry

router.register(r'subsystems', SubsystemViewSet)

您可以找到路由器使用情况here的示例。