[Django]-Django REST Framework: Could not resolve URL for hyperlinked relationship using view name

8πŸ‘

βœ…

The view_name should typically be [route]-detail for routers, where [route] is the name of the model you registered the ViewSet under.

In your case, the view_name should be position-detail, not just position. You are also using routes-detail instead of drivingroutes-detail, which is using the long name because your model is DrivingRoute and not Route. You can override this by setting a base_name (third parameter) when using register on the router.

router = DefaultRouter()
router.register(r'car', views.CarViewSet)
router.register(r'routes', views.DrivingRouteViewSet, "routes")
router.register(r'position', views.PositionViewSet)
router.register(r'users', views.UserViewSet)

1πŸ‘

You have the following views:

  • car-list
  • car-detail
  • drivingroute-list
  • drivingroute-detail
  • position-list
  • position-detail
  • user-list
  • user-detail

If we take for example the following route:

router.register(r'car', views.CarViewSet)

It means that it is accessible under http://yourapi.com/car/ for car-list and under http://yourapi.com/car/1/ for car-detail (where 1 is the id).

The view names get built out of the class name minus suffix ViewSet plus list or detail.

Change your code according to these rules and please try again.

Cheers!

πŸ‘€cezar

Leave a comment