Ошибка типа в as_view () в Django Restful Application - PullRequest
0 голосов
/ 03 мая 2020

У меня есть базовое c Django приложение, которое я частично копирую из документов Django Rest Framework.

Проблема, с которой я столкнулся, заключалась в реализации обобщенного c на основе класса views Я получаю странную ошибку.

TypeError: as_view() takes 1 positional argument but 2 were given

Трассировка стека указывает на мой файл api/urls.py и эту строку, где я определяю шаблоны URL:

path('', include(router.urls)),

Проект основан на api со вторым приложением users.

Единственное место, в котором реализован as_view(), находится в users/urls.py следующим образом:

from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from users import views

urlpatterns = [
    path('users/', views.UserList.as_view()),
    path('users/<int:pk>', views.UserDetail.as_view()),
]

urlpatterns = format_suffix_patterns(urlpatterns)

Какие аргументы должны быть там или что я неправильно настроил, что вызывает эту проблему?

views.py

from users.models import User
from users.serializers import UserSerializer
from rest_framework import generics


class UserList(generics.ListCreateAPIView):
    """
    List all users or create a new user
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer

    @classmethod
    def get_extra_actions(cls):
        return []


class UserDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

api/urls.py

from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from users import views

router = routers.DefaultRouter()
router.register(r'users', views.UserList)
router.register(r'users', views.UserDetail)

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

Stack Trace

etherk1ll@ubuntu:~/Development/python_projects/news-rest/api$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 395, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 407, in check
    for pattern in self.url_patterns:
  File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 588, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 581, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.6/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 "/home/etherk1ll/Development/python_projects/news-rest/api/api/urls.py", line 26, in <module>
    path('', include(router.urls)),
  File "/usr/local/lib/python3.6/dist-packages/rest_framework/routers.py", line 78, in urls
    self._urls = self.get_urls()
  File "/usr/local/lib/python3.6/dist-packages/rest_framework/routers.py", line 339, in get_urls
    urls = super().get_urls()
  File "/usr/local/lib/python3.6/dist-packages/rest_framework/routers.py", line 266, in get_urls
    view = viewset.as_view(mapping, **initkwargs)
TypeError: as_view() takes 1 positional argument but 2 were given
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...