0

I've setup a Django REST framework project but the api root hasn't been populated with anything eg a users ViewSet and I can't access the expected url with a list of users.

There is one app users, with a custom user model. (and the django project is named api)

main urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('users.urls')),
]

the users app urls.py

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

from users.views import CustomUserViewSet

router = DefaultRouter()
router.register("users", CustomUserViewSet, 'users')

urlpatterns = [
]

urlpatterns += router.urls

users models.py

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    pass

    def __str__(self):
        return self.username

users serialisers.py

from rest_framework.serializers import ModelSerializer
from .models import CustomUser


class CustomUserSerializer(ModelSerializer):
    class Meta:
        model = CustomUser
        fields = '__all__'

users app views.py

from django.shortcuts import render
from rest_framework.viewsets import ViewSet
from .serializers import CustomUserSerializer
from .models import CustomUser


class CustomUserViewSet(ViewSet):
    serializer_class = CustomUserSerializer
    queryset = CustomUser.objects.all(

And empty api root at localhost:8000/api/ enter image description here

and 404 error at localhost:8000/api/users/ enter image description here

2 Answers 2

2

If you are using just Viewset, you need to implement the actions.

From the docs:

The ViewSet class does not provide any implementations of actions. In order to use a ViewSet class you'll override the class and define the action implementations explicitly

You can add a list action like in the example:

from rest_framework.response import Response
class CustomUserViewSet(ViewSet):
    serializer_class = CustomUserSerializer
    queryset = CustomUser.objects.all()

    def list(self, request):
        queryset = CustomUser.objects.all()
        serializer = CustomUserSerializer(queryset, many=True)
        return Response(serializer.data)

Or (maybe what you are looking for) using something like ModelViewSet that already includes implementations for various actions:

class CustomUserViewSet(viewsets.ModelViewSet):
    serializer_class = CustomUserSerializer
    queryset = CustomUser.objects.all()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks what a silly error it was ModelViewSet I meant to put in. Thanks.
0

In your case, you have to simply replace

class CustomUserViewSet(ViewSet):

with

class CustomUserViewSet(viewsets.ModelViewSet):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.