Django Rest Framework 反向和 SimpleRouter

Django Rest Framework reverse and SimpleRouter

如何使用 DRF 的 reverseSimpleRouter 调用复杂的 URL?

我的URL在两个地方,一个是团队,一个是游戏,如下:

league.urls:

url(r'^team/', include('teams.urls')),

team.urls:

router = SimpleRouter()
router.register(r'game', GameViewSet, 'games')

我正在尝试 reverse url 更新游戏。基于 DRF SimpleRouter,这应该是“/team/{pk}/game/{pk}”

我的测试正在调用:

url = reverse('games-detail', args=[team.pk, game.pk])

但我收到以下错误:

    raise error, v # invalid expression
error: redefinition of group name u'pk' as group 2; was group 1

YPCrumble,您可能希望用不同的 kwargs 调用 URLURL 正则表达式以处理 kwargs 的方式工作。例如:

# python reverse url
url = reverse('games-detail', kwargs={'team_pk': 1, 'group_pk':1})

# url regex
url(
    r'^team/(?P<team_pk>\d+)/group/(?P<group_pk>\d+)/$',
    view.SimpleRouterDetailView.as_view(),
    name='games-detail'
)