Home > OS >  rest_framework returns Not found on url users/1/
rest_framework returns Not found on url users/1/

Time:12-28

I need to return via rest_framework a users's detail In my views.py i have UserViewSet class with

class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
pagination_class = LimitOffsetPagination
# permission_classes = (IsAdmin, )
search_fields = ('username',)
lookup_field = 'username'

my urls.py has this route

from rest_framework.routers import DefaultRouter
from .views import UserViewSet
router = DefaultRouter()
router.register(r'users', UserViewSet)
urlpatterns = [
    path('auth/', include(auth_patterns)),
    path('', include(router.urls)),
]

Postman returns Not found - what im doing wrong? enter image description here

CodePudding user response:

If you set lookup_field to username, you should use 'username' instead of ID. So for example if your username is admin, use api/users/admin. If you want to query it by the key, remove lookup_field entirely. That way, it will use the ID by default.

  • Related