Django-rest-framework: dot character excluded from viewset lookup_field url conf

Created on 6 Nov 2014  ·  4Comments  ·  Source: encode/django-rest-framework

Somewhere between djangorestframework==2.3.13 and djangorestframework==2.4.4 the generated url conf for a ModelViewSet has changed to exclude '.' (period / dot) character. This means that your custom lookup_field cannot be a domain name, or some other string that includes this character.

Given a view set such as:

class ZoneViewSet(viewsets.ModelViewSet):
    lookup_field = 'domain__name'

The following urls are generated (djangorestframework==2.3.13):

^api/ ^v1/ ^zones/(?P<domain__name>[^/]+)/$ [name='zone-detail']
^api/ ^v1/ ^zones/(?P<domain__name>[^/]+)/\.(?P<format>[a-z0-9]+)$ [name='zone-detail']

And now with djangorestframework==2.4.4:

^api/ ^v1/ ^zones//$ [name='zone-detail']
^api/ ^v1/ ^zones/(?P<domain__name>[^/.]+)/\.(?P<format>[a-z0-9]+)$ [name='zone-detail']

The [^] specifies an exclusion set, so [^/] excludes all forward slashes, whilst [^/.]excludes forward slashes and period (dot) characters.

I believe there is no reason to restrict periods in URIs and this change should be reverted.

Most helpful comment

And here's the commit that broke this previous functionality https://github.com/tomchristie/django-rest-framework/commit/3cd15fb1713dfc49e1bf1fd48045ca3ae5654e18. The commit log shows that you're aware of the issue. I presume you don't intend to change it, so I'll close this issue.

Anyone who finds this be aware that in order to allow periods in urlconf generated by the default router, you need to add lookup_value_regex = '[^/]+' to your viewset.

All 4 comments

And here's the commit that broke this previous functionality https://github.com/tomchristie/django-rest-framework/commit/3cd15fb1713dfc49e1bf1fd48045ca3ae5654e18. The commit log shows that you're aware of the issue. I presume you don't intend to change it, so I'll close this issue.

Anyone who finds this be aware that in order to allow periods in urlconf generated by the default router, you need to add lookup_value_regex = '[^/]+' to your viewset.

I wonder why dot was excluded in the first place...

Iirc dots interfer with the ability to add extension content type within urls - ie /myendpoint.json

And here's the commit that broke this previous functionality 3cd15fb. The commit log shows that you're aware of the issue. I presume you don't intend to change it, so I'll close this issue.

Anyone who finds this be aware that in order to allow periods in urlconf generated by the default router, you need to add lookup_value_regex = '[^/]+' to your viewset.

Thank you very very much!

Was this page helpful?
0 / 5 - 0 ratings