Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a reverse in DefaultRouter()

I'm setting up a new tests, and i want to make an reverse.

router = DefaultRouter()
router.register('profile', views.UserProfileViewSet, base_name='profile')

urlpatterns = [
   url(r'', include(router.urls))
]

UserProfileViewSet

class UserProfileViewSet(viewsets.ModelViewSet):
   """Handles creating, creating and updating profiles."""

   serializer_class = serializers.UserProfileSerializer
   permission_classes = (permissions.UpdateOwnProfile,)
   authentication_classes = (TokenAuthentication,)

   queryset = get_user_model().objects.all()

So , i want make a reverse in tests.py. my shot is:

CREAT_USER_URL = reverse('profile-create')

And I simply get:

Reverse for 'profile-create' not found. 'profile-create' is not a valid view function or pattern name.

How should I set up a reverse in this case.

like image 656
Noah Lc Avatar asked Jan 05 '19 02:01

Noah Lc


1 Answers

You should use profile-list instead of profile-create

CREAT_USER_URL = reverse('profile-list')

There is no URL as {base_name}-create, If you wanna use create endpoint, use {base_name}-list.

For more information, refer to this table

like image 64
JPG Avatar answered Oct 26 '22 07:10

JPG