diff --git a/rest_framework/routers.py b/rest_framework/routers.py index de04cb674..2c24f9099 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -100,6 +100,10 @@ class BaseRouter(six.with_metaclass(RenameRouterMethods)): basename = self.get_default_basename(viewset) self.registry.append((prefix, viewset, basename)) + # invalidate the urls cache + if hasattr(self, '_urls'): + del self._urls + def get_default_basename(self, viewset): """ If `basename` is not specified, attempt to automatically determine diff --git a/tests/test_routers.py b/tests/test_routers.py index eae3f5458..c74055347 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -158,6 +158,12 @@ class TestSimpleRouter(URLPatternsTestCase, TestCase): response = self.client.delete(reverse('basic-action3', args=[1])) assert response.data == {'delete': '1'} + def test_register_after_accessing_urls(self): + self.router.register(r'notes', NoteViewSet) + assert len(self.router.urls) == 2 # list and detail + self.router.register(r'notes_bis', NoteViewSet) + assert len(self.router.urls) == 4 + class TestRootView(URLPatternsTestCase, TestCase): urlpatterns = [