encode / django-rest-framework

Web APIs for Django. 🎸
https://www.django-rest-framework.org
Other
28.21k stars 6.81k forks source link

Router.register cannot merge with urlpatters #9366

Closed iasknew closed 5 months ago

iasknew commented 6 months ago

In version 3.14, there will be no error, while in version 3.15, this situation will result in an error: router.register('task', CeleryTaskModelViewSet) urlpatterns = [ path('task/job_list/', CeleryTaskModelViewSet.as_view({'get': 'job_list'})), path('task/update_status//', CeleryTaskModelViewSet.as_view({'post': 'update_status'})), path('task/delete_task//', CeleryTaskModelViewSet.as_view({'delete': 'destroy'})), ] urlpatterns += router.urls

browniebroke commented 6 months ago

What "error"? Would be helpful if you could share the message and/or trace back

iasknew commented 5 months ago

[2024-04-06 17:26:58][django.utils.autoreload.run_with_reloader():668] [INFO] Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last): File "C:\Users\iasknew\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\Users\iasknew\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run self._target(*self._args, *self._kwargs) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper fn(args, **kwargs) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\channels\management\commands\runserver.py", line 75, in inner_run self.check(display_num_errors=True) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\core\management\base.py", line 486, in check all_issues = checks.run_checks( File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks new_errors = check(app_configs=app_configs, databases=databases) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\core\checks\urls.py", line 42, in check_url_namespaces_unique all_namespaces = _load_all_namespaces(resolver) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\core\checks\urls.py", line 61, in _load_all_namespaces url_patterns = getattr(resolver, "url_patterns", []) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\utils\functional.py", line 47, in get res = instance.dict[self.name] = self.func(instance) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\urls\resolvers.py", line 738, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\utils\functional.py", line 47, in get res = instance.dict[self.name] = self.func(instance) File "C:\Projects\FunXpress\EsAdmin\venv\lib\site-packages\django\urls\resolvers.py", line 731, in urlconf_module return import_module(self.urlconf_name) File "C:\Users\iasknew\AppData\Local\Programs\Python\Python310\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1050, in _gcd_import File "", line 1027, in _find_and_load File "", line 1006, in _find_and_load_unlocked File "", line 688, in _load_unlocked File "", line 883, in exec_module File "", line 241, in _call_with_frames_removed File "C:\Projects\FunXpress\EsAdmin\application\urls.py", line 91, in

browniebroke commented 5 months ago

Yes, that's a protection that was added in 3.15: https://github.com/encode/django-rest-framework/pull/8438

You can pass a basename like this:

router.register('task', CeleryTaskModelViewSet, basename='something-unique')

This basename is used when calling reverse() and makes sure that you're reversing the correct URL.

I wonder why you need to register extra URLs as individual views, though. It sounds like you may benefit from viewsets actions instead: https://www.django-rest-framework.org/api-guide/viewsets/#viewset-actions