mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-10-30 23:47:53 +03:00 
			
		
		
		
	Router: Do not automatically adjust lookup_regex when trailing_slash is True
BREAKING CHANGE When trailing_slash is set to True, the router no longer will adjust the lookup regex to allow it to include periods. To simulate the old behavior, the programmer should specify `lookup_regex = '[^/]+'` on the viewset. https://github.com/tomchristie/django-rest-framework/pull/1328#issuecomment-31517099
This commit is contained in:
		
							parent
							
								
									a1d7aa8f71
								
							
						
					
					
						commit
						3cd15fb171
					
				|  | @ -83,7 +83,7 @@ This behavior can be modified by setting the `trailing_slash` argument to `False | ||||||
| 
 | 
 | ||||||
| Trailing slashes are conventional in Django, but are not used by default in some other frameworks such as Rails.  Which style you choose to use is largely a matter of preference, although some javascript frameworks may expect a particular routing style. | Trailing slashes are conventional in Django, but are not used by default in some other frameworks such as Rails.  Which style you choose to use is largely a matter of preference, although some javascript frameworks may expect a particular routing style. | ||||||
| 
 | 
 | ||||||
| With `trailing_slash` set to True, the router will match lookup values containing any characters except slashes and dots. When set to False, dots are allowed.  To restrict the lookup pattern, set the `lookup_field_regex` attribute on the viewset.  For example, you can limit the lookup to valid UUIDs: | The router will match lookup values containing any characters except slashes and period characters.  For a more restrictive (or lenient) lookup pattern, set the `lookup_field_regex` attribute on the viewset.  For example, you can limit the lookup to valid UUIDs: | ||||||
| 
 | 
 | ||||||
|     class MyModelViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet): |     class MyModelViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet): | ||||||
|         lookup_field = 'my_model_id' |         lookup_field = 'my_model_id' | ||||||
|  |  | ||||||
|  | @ -224,11 +224,8 @@ class SimpleRouter(BaseRouter): | ||||||
|         try: |         try: | ||||||
|             lookup_value = viewset.lookup_value_regex |             lookup_value = viewset.lookup_value_regex | ||||||
|         except AttributeError: |         except AttributeError: | ||||||
|             if self.trailing_slash: |             # Don't consume `.json` style suffixes | ||||||
|                 lookup_value = '[^/]+' |             lookup_value = '[^/.]+' | ||||||
|             else: |  | ||||||
|                 # Don't consume `.json` style suffixes |  | ||||||
|                 lookup_value = '[^/.]+' |  | ||||||
|         return base_regex.format( |         return base_regex.format( | ||||||
|             lookup_prefix=lookup_prefix, |             lookup_prefix=lookup_prefix, | ||||||
|             lookup_field=lookup_field, |             lookup_field=lookup_field, | ||||||
|  |  | ||||||
|  | @ -152,7 +152,7 @@ class TestTrailingSlashIncluded(TestCase): | ||||||
|         self.urls = self.router.urls |         self.urls = self.router.urls | ||||||
| 
 | 
 | ||||||
|     def test_urls_have_trailing_slash_by_default(self): |     def test_urls_have_trailing_slash_by_default(self): | ||||||
|         expected = ['^notes/$', '^notes/(?P<pk>[^/]+)/$'] |         expected = ['^notes/$', '^notes/(?P<pk>[^/.]+)/$'] | ||||||
|         for idx in range(len(expected)): |         for idx in range(len(expected)): | ||||||
|             self.assertEqual(expected[idx], self.urls[idx].regex.pattern) |             self.assertEqual(expected[idx], self.urls[idx].regex.pattern) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user