Ubuntu LTS, Python 3.6.x, Django 2.2.1, DRF 3.9.3
I started with the DRF quickstart. Worked fine.
Jumped into the DRF tutorial, step 3 on using generic class based views I'm getting this error when trying 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'
I've only made the minimal changes to my code base in order to implement the recommendations by the tutorial.
It's not obvious what I've done wrong, and there's not a lot of information around get_extra_actions
when I read the ListCreateAPIView guide, and not a lot that seems obviously comparable when looking elsewhere
urls.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
Note/Edit: I've found some questions, but few solutions.
Basically you are trying to register a non-viewset(here InstanceList
a view, not a viewset) to router. Instead of this, you can simply use it in urls like this:
router = routers.DefaultRouter()
router.register(r'users',views.UserViewSet)
urlpatterns = [
path('', views.dash, name='dash'),
path('api/', include(router.urls)),
path('api/instances/', views.InstanceList.as_view(), name="instances"),
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With