Skip to content

Commit 40c204b

Browse files
author
Sébastien Diemer
committed
routers: invalidate _urls cache on register
see #5660 Trying to register new routes on a router after having accessed the router `urls` attribute leads to surprising results. The route is added without error to the router's `registry` but the urls are not updated, because they are cached in `_urls`. This commit invalidates the cache after each new registration.
1 parent c049777 commit 40c204b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

rest_framework/routers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ def register(self, prefix, viewset, basename=None, base_name=None):
100100
basename = self.get_default_basename(viewset)
101101
self.registry.append((prefix, viewset, basename))
102102

103+
# invalidate the urls cache
104+
if hasattr(self, '_urls'):
105+
del self._urls
106+
103107
def get_default_basename(self, viewset):
104108
"""
105109
If `basename` is not specified, attempt to automatically determine

tests/test_routers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ def test_multiple_action_handlers(self):
158158
response = self.client.delete(reverse('basic-action3', args=[1]))
159159
assert response.data == {'delete': '1'}
160160

161+
def test_register_after_accessing_urls(self):
162+
self.router.register(r'notes', NoteViewSet)
163+
assert len(self.router.urls) == 2 # list and detail
164+
self.router.register(r'notes_bis', NoteViewSet)
165+
assert len(self.router.urls) == 4
166+
161167

162168
class TestRootView(URLPatternsTestCase, TestCase):
163169
urlpatterns = [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy