Django如何处理Django Rest Framework的DefaultRouter

时间:2015-08-20 04:27:17

标签: python django django-rest-framework

我试图弄清楚Django如何处理Django Rest Framework的DefaultRouter

以下是DefaultRouter

的用例
from django.conf.urls import url, include
from snippets import views
from rest_framework.routers import DefaultRouter

# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'snippets', views.SnippetViewSet)
router.register(r'users', views.UserViewSet)

# The API URLs are now determined automatically by the router.
# Additionally, we include the login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

DefaultRouter会从router.urls

返回urls列表
def get_urls(self):
    """
    Use the registered viewsets to generate a list of URL patterns.
    """
    ret = []

    for prefix, viewset, basename in self.registry:
        lookup = self.get_lookup_regex(viewset)
        routes = self.get_routes(viewset)

        for route in routes:

            # Only actions which actually exist on the viewset will be bound
            mapping = self.get_method_map(viewset, route.mapping)
            if not mapping:
                continue

            # Build the url pattern
            regex = route.url.format(
                prefix=prefix,
                lookup=lookup,
                trailing_slash=self.trailing_slash
            )
            view = viewset.as_view(mapping, **route.initkwargs)
            name = route.name.format(basename=basename)
            ret.append(url(regex, view, name=name))

    return ret

然而,似乎包含function并没有真正分解网址列表

其他一些代码是否会进行分解?

我认为include函数可以执行类似下面的操作

for url_instance in arg:
    # perform the include logic for each url_instance

我想我错过了一些部分,任何提示都会受到赞赏

由于

0 个答案:

没有答案