Yes, it's possible to write this code without using regexp, and it should 
work the same.

If you have a look at the source code of the routers, there's a method 
`get_urls` in SimpleRouter (you can find it here 
https://github.com/encode/django-rest-framework/blob/master/rest_framework/routers.py),
 
which, among others, does:

regex = route.url.format(
    prefix=prefix,
    lookup=lookup,
    trailing_slash=self.trailing_slash
)


where `route.url` is something like 

url=r'^{prefix}/{lookup}{trailing_slash}$',


and `prefix` is the first argument you pass to `router.register` - in your 
case, an empty string:

def register(self, prefix, viewset, basename=None, base_name=None)


It does not seem to matter whether you put `prefix=r''` or `prefix=''`, the 
ultimate url regex looks the same.
Disclaimer: I assume you're using the latest version of DRF (3.9.x); I 
haven checked how the code looks in older versions. 

On Wednesday, 22 May 2019 17:20:20 UTC+2, Rounak Jain wrote:

>
> I am using DRF Viewsets to auto-generate URLs for different views. Is it 
> possible to write the code below without using regex?
> Thanks
>
> from .views import TaskViewSet
> from rest_framework.routers import DefaultRouter
>
> router = DefaultRouter()
> router.register(r'', TaskViewSet, basename='task')
> urlpatterns = router.urls
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e929c4c9-a174-434d-bda9-9000e5cf8892%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to