Reputation: 101
So, I would like top stop ursing urlpatterns
and just use router
. But instead of using ID of an object I'm using UUID instead and I'm using it with urlpatterns and dont't find some way to use it with routers
.
this is my current model:
class Board(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=200, blank=False, null=False)
this is my core app urls.py
:
...
router = DefaultRouter()
router.register(r'boards', BoardViewSet)
router.register(r'y', yViewSet)
router.register(r'z', zViewSet, basename='z')
urlpatterns = [
path('', include(router.urls)),
path('board-list/<uuid:pk>/', BoardViewSet.as_view({'get': 'list'}), name='boards'),
]
and this is the project urls.py
:
from django.contrib import admin
from django.urls import path, include
from core.urls import router as api_router
routes = []
routes.extend(api_router.urls)
urlpatterns = [
path('api/', include((routes, 'board_microservice'), namespace='v1')),
path('admin/', admin.site.urls),
]
the application usage is ok, but I have some troubles with test. i.e: this works well:
url = reverse('v1:board-list')
response = api_client().get(
url
)
and it isn't working:
board = baker.make(Board)
url = reverse('v1:board-list', kwargs={"pk": board.id})
response = api_client().get(url)
I receive
django.urls.exceptions.NoReverseMatch: Reverse for 'board-list' with keyword arguments
and I think I can replace urlpatterns
by router
to solve it and turns it more simple
There is any way to do it with router?
Upvotes: 1
Views: 1331
Reputation: 12068
You haven't shared your view, but it seems you are using a ModelViewSet
and you seem to be looking for the retrieve
endpoint rather than list
(judging from using a pk
). If so then you want to use -detail
instead of -list
:
url = reverse('v1:board-detail', kwargs={"pk": board.id})
Upvotes: 2
Reputation: 660
Board_list is not what you want to call... Try board_get instead.
Board list takes no argument. But get does.. To get a particular board.
Upvotes: 0