mercredi 28 août 2019

How to reverse path for custom user when testing Django Rest Framework

I'm building a Django 3 app using a Custom User. I'm trying to test the Custom User, but I can't get the url using reverse. I'm using Django Rest Framework. I'm using Django Rest Auth.

myapp.api.urls.py:

app_name = 'myApp'

from django.urls import include, path, re_path

urlpatterns = [
...
path('users/', include('users.urls'), name='UsersURLS'),
]

myapp.users.urls.py

from django.urls import include, path

from . import api
app_name = 'users' # namespace for reverse
urlpatterns = [
    path('', api.UserListView.as_view(), name='UsersPath'),
]

myapp.users.api.py

class UserListView(generics.ListCreateAPIView):
    queryset = models.CustomUser.objects.all()
    serializer_class = serializers.UserSerializer
    authentication_classes = (TokenAuthentication,)

And in test_users_api.py:

from users.api import UserListView

user_detail_url = reverse('myApp:UsersURLS-list')

...

def test_user_detail(self):
    self.client.force_authenticate(user=self.user)
    response = self.client.post(user_detail_url, {'pk': self.user.id }, format='json')

Whatever I try for the reverse, I get an error that is it not a valid view or function name

reverse('myApp:UsersURLS-list')

reverse('users:UsersPath-list')

reverse(UserListView)

I'd be grateful for any idea what I'm doing wrong, and how to get the reverse URL. I have it working for the rest of my endpoints which use router, but I can't see what's needed for my Custom User, which I set up following a tutorial and it works fine otherwise.

Aucun commentaire:

Enregistrer un commentaire