drf router routing learning & detail_route and list_route decorators

API reference

SimpleRouter

SimpleRouterIt contains standard list, create, retrieve, update, partial_updateand destroyaction. SimpleRouterAlso supports the view set used @detail_routeor @list_routedecorative tags to be routed to other methods.

URL Style HTTP Method Action URL Name
{prefix}/ GET list {basename}-list
POST create
{prefix}/{methodname}/ GET, or specified by the `methods` parameter `@list_route` method of decoration {basename}-{methodname}
{prefix}/{lookup}/ GET retrieve {basename}-detail
PUT update
PATCH partial_update
DELETE destroy
{prefix}/{lookup}/{methodname}/ GET, or specified by the `methods` parameter `@detail_route` method of decoration {basename}-{methodname}

By default, the SimpleRouterURL is created added the trailing slash. When instantiating a router, you may be prepared by trailing_slashparameter to Falsemodify this behavior. E.g:

router = SimpleRouter(trailing_slash=False)

The trailing slash is normal in Django, but not used by default in some other frameworks (such as Rails). The choice of which style to use is largely a matter of preference, although some JavaScript frameworks may expect a specific routing style.

SimpleRouterWill match lookup values ​​that contain any characters except slash and period characters. For more stringent (or relaxed) a lookup pattern, set on a set of views lookup_value_regexproperties. For example, you can restrict lookups to valid UUIDs:

class MyModelViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    lookup_field = 'my_model_id'
    lookup_value_regex = '[0-9a-f]{32}'

DefaultRouter

DefaultRouterThe above and SimpleRoutersimilar, but also contains a default API root view, the view returns a response to pointing to hyperlinks containing a list of all views. It is optional .jsonto generate routing style format suffix.

URL Style HTTP Method Action URL Name
[.format] GET Automatically generated root view api-root
{prefix}/[.format] GET list {basename}-list
POST create
{prefix}/{methodname}/[.format] GET, or specified by the `methods` parameter `@list_route` method of decoration {basename}-{methodname}
{prefix}/{lookup}/[.format] GET retrieve {basename}-detail
PUT update
PATCH partial_update
DELETE destroy
{prefix}/{lookup}/{methodname}/[.format] GET, or specified by the `methods` parameter `@detail_route` method of decoration {basename}-{methodname}

After the lookup_field is specified, if there is no list_route decorator, the format of the url is {prefix}/{lookup}/[.format], that is , the value of lookup_field will be added to the url.

You can see that the method of detail_route decoration will use the lookup_field defined by the view set and add it to the url.
list_route will not.

View the URL of the corresponding view through reverse

from django.urls import reverse
reverse('result_table_line-detail', args={
    
    'sample_set_id': 999, 'result_table_id': '591_cnt_1min'})
>>> '/v3/aiops/sample_set/sample_set_id/result_table/result_table_id/'

result_table_line-detail is the base_name when router is registered, and -detail refers to the above table.

reference

Chinese translation of drf document—router part Chinese translation of
drf document—Viewset view set part

Guess you like

Origin blog.csdn.net/qq_42648305/article/details/112753864