Python社区  »  Django

Django REST Framework TypeError:register()缺少1个必需的位置参数:“viewset”

Anibal Cardozo • 4 周前 • 106 次点击  

我正在学习Django REST框架,我正在尝试使用一个简单的视图集,但是当我试图运行服务器时,控制台上一直出现这个错误

  File "C:\Users\anahu\Projects\guatudu-api\api\api\locations\urls.py", line 13, in <module>
    router.register(r'countries', country_views.CountryViewSet, basename='country')
TypeError: register() missing 1 required positional argument: 'viewset'

这是我的应用程序的url.py

"""Locations Urls"""

# Django
from django.urls import path, include

# Django Rest Framework
from rest_framework.routers import DefaultRouter

# Views
from api.locations.views import countries as country_views

router = DefaultRouter
router.register(r'countries', country_views.CountryViewSet, basename='country')

urlpatterns = router.urls

这是我的视图集

"""Countries view"""

# Django REST Framework
from rest_framework import viewsets

# Serializers
from api.locations.serializers import CountryModelSerializer

# Models
from api.locations.models import Country

class CountryViewSet(viewsets.ModelViewSet):
    """Country viewset"""

    queryset = Country.objects.all()
    serializer_class = CountryModelSerializer

"""Country Serializers"""

#Django Rest Framework
from rest_framework import serializers
from rest_framework.validators import UniqueValidator

#Model
from api.locations.models import Country

class CountryModelSerializer(serializers.ModelSerializer):
    """Country Model Serializer"""

    class Meta:
        """Meta class"""
        model = Country
        fields = (
            'id',
            'name',
            'image'
        )

是很基本的东西,但我一直有这个错误。我能想象的是,由于某种原因,我没有从url.py上正确地获取视图集?希望你们能帮我

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53263
 
106 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 4 周前
Gorkhali Khadka
Reply   •   1 楼
Gorkhali Khadka    4 周前

尝试更改您的url:

"""Locations Urls"""

# Django
from django.urls import path, include

# Django Rest Framework
from rest_framework.routers import DefaultRouter

# Views
from api.locations.views import countries as country_views

router = DefaultRouter()
router.register(r'countries', country_views.CountryViewSet, basename='country')

urlpatterns = router.urls