The default router extends the SimpleRouter, but also adds in a default API root view, and adds format suffix patterns to the URLs.
| 342 | |
| 343 | |
| 344 | class DefaultRouter(SimpleRouter): |
| 345 | """ |
| 346 | The default router extends the SimpleRouter, but also adds in a default |
| 347 | API root view, and adds format suffix patterns to the URLs. |
| 348 | """ |
| 349 | include_root_view = True |
| 350 | include_format_suffixes = True |
| 351 | root_view_name = 'api-root' |
| 352 | default_schema_renderers = None |
| 353 | APIRootView = APIRootView |
| 354 | APISchemaView = SchemaView |
| 355 | SchemaGenerator = SchemaGenerator |
| 356 | |
| 357 | def __init__(self, *args, **kwargs): |
| 358 | if 'root_renderers' in kwargs: |
| 359 | self.root_renderers = kwargs.pop('root_renderers') |
| 360 | else: |
| 361 | self.root_renderers = list(api_settings.DEFAULT_RENDERER_CLASSES) |
| 362 | super().__init__(*args, **kwargs) |
| 363 | |
| 364 | def get_api_root_view(self, api_urls=None): |
| 365 | """ |
| 366 | Return a basic root view. |
| 367 | """ |
| 368 | api_root_dict = {} |
| 369 | list_name = self.routes[0].name |
| 370 | for prefix, viewset, basename in self.registry: |
| 371 | api_root_dict[prefix] = list_name.format(basename=basename) |
| 372 | |
| 373 | return self.APIRootView.as_view(api_root_dict=api_root_dict) |
| 374 | |
| 375 | def get_urls(self): |
| 376 | """ |
| 377 | Generate the list of URL patterns, including a default root view |
| 378 | for the API, and appending `.json` style format suffixes. |
| 379 | """ |
| 380 | urls = super().get_urls() |
| 381 | |
| 382 | if self.include_root_view: |
| 383 | view = self.get_api_root_view(api_urls=urls) |
| 384 | root_url = path('', view, name=self.root_view_name) |
| 385 | urls.append(root_url) |
| 386 | |
| 387 | if self.include_format_suffixes: |
| 388 | urls = format_suffix_patterns(urls) |
| 389 | |
| 390 | return urls |
no outgoing calls