routers.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:django-rest-witchcraft 作者: shosca 项目源码 文件源码
def get_lookup_regex(self, viewset, lookup_prefix=''):
        """
        Given a viewset, return the portion of the url regex that is used to match against a single instance.

        Can be overwritten by providing a `lookup_url_regex` on the viewset.
        """

        lookup_url_regex = getattr(viewset, 'lookup_url_regex', None)
        if lookup_url_regex:
            return lookup_url_regex

        model = getattr(viewset, 'get_model', lambda: None)()
        if model:
            info = model_info(model)
            base_regex = '(?P<{lookup_prefix}{lookup_url_kwarg}>{lookup_value})'

            regexes = []
            for key, _ in info.primary_keys.items():
                regexes.append(
                    base_regex.format(lookup_prefix=lookup_prefix, lookup_url_kwarg=key, lookup_value='[^/.]+')
                )

            return '/'.join(regexes)

        return super(DefaultRouter, self).get_lookup_regex(viewset, lookup_prefix)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号