Ubuntu LTS,Python 3.6.x,Django 2.2.1,DRF 3.9.3
我从DRF快速入门开始。效果不错。
跳转到DRF教程,第3步使用基于泛型类的视图我得到这个错误时,尝试manage.py runserver
File "/home/ubuntu/src/rasi/rais/instances/urls.py", line 11, in <module>
path('api/', include(router.urls)),
File "/home/ubuntu/.virtualenv/managed-research-prod/lib/python3.6/site-packages/rest_framework/routers.py", line 125, in
urls
self._urls = self.get_urls()
File "/home/ubuntu/.virtualenv/managed-research-prod/lib/python3.6/site-packages/rest_framework/routers.py", line 386, in
get_urls
urls = super(DefaultRouter, self).get_urls()
File "/home/ubuntu/.virtualenv/managed-research-prod/lib/python3.6/site-packages/rest_framework/routers.py", line 284, in
get_urls
routes = self.get_routes(viewset)
File "/home/ubuntu/.virtualenv/managed-research-prod/lib/python3.6/site-packages/rest_framework/routers.py", line 200, in
get_routes
extra_actions = viewset.get_extra_actions()
AttributeError: type object 'InstanceList' has no attribute 'get_extra_actions'
我只对我的代码库做了最小的修改,以实现教程中的建议。
我做错了什么并不明显,当我阅读ListCreateAPIView指南时,关于get_extra_actions
的信息并不多,当我在其他地方查找时,似乎也没有太多明显可比的信息
url.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'users',views.UserViewSet)
router.register(r'instances',views.InstanceList)
urlpatterns = [
path('', views.dash, name='dash'),
path('api/', include(router.urls)),
]
models.py
class User(AbstractUser):
email = models.EmailField()
dept = models.CharField(max_length=400)
display_name = models.CharField(max_length=200)
class Instance(models.Model):
name = models.CharField(max_length=200)
flavour = models.CharField(choices=FLAVOUR_CHOICES, max_length=20)
os = models.IntegerField(choices=OS_CHOICES)
hostname = models.CharField(max_length=200)
allocation_manager = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='manager')
allocation_notes = models.TextField(blank=True,null=True)
requested_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='requestor')
serializer.py
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
class InstanceSerializer(serializers.HyperlinkedModelSerializer):
allocation_manager = UserSerializer()
requested_by = UserSerializer()
class Meta:
model = Instance
fields = ('name', 'flavour', 'hostname', 'allocation_manager','requested_by')
views.py
# Serializer Views
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
class InstanceList(generics.ListCreateAPIView):
queryset = Instance.objects.all()
serializer_class = InstanceSerializer
注意/编辑:我找到了some questions, but few solutions。
2条答案
按热度按时间mccptt671#
基本上,您正在尝试将非视图集(这里的
InstanceList
是视图,而不是视图集)注册到路由器。相反,你可以简单地在url中使用它,像这样:ntjbwcob2#
为什么你需要视图的序列化器?