diff options
Diffstat (limited to 'docs/api-guide')
| -rw-r--r-- | docs/api-guide/fields.md | 2 | ||||
| -rw-r--r-- | docs/api-guide/format-suffixes.md | 12 | ||||
| -rwxr-xr-x | docs/api-guide/generic-views.md | 4 | ||||
| -rw-r--r-- | docs/api-guide/relations.md | 1 | ||||
| -rw-r--r-- | docs/api-guide/routers.md | 50 | ||||
| -rw-r--r-- | docs/api-guide/serializers.md | 2 | 
6 files changed, 69 insertions, 2 deletions
| diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index f06db56c..946e355d 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -480,7 +480,7 @@ Let's look at an example of serializing a class that represents an RGB color val      class ColorField(serializers.Field):          """ -        Color objects are serialized into "rgb(#, #, #)" notation. +        Color objects are serialized into 'rgb(#, #, #)' notation.          """          def to_representation(self, obj):              return "rgb(%d, %d, %d)" % (obj.red, obj.green, obj.blue) diff --git a/docs/api-guide/format-suffixes.md b/docs/api-guide/format-suffixes.md index 20c1e995..35dbcd39 100644 --- a/docs/api-guide/format-suffixes.md +++ b/docs/api-guide/format-suffixes.md @@ -55,6 +55,18 @@ The name of the kwarg used may be modified by using the `FORMAT_SUFFIX_KWARG` se  Also note that `format_suffix_patterns` does not support descending into `include` URL patterns. +### Using with `i18n_patterns` + +If using the `i18n_patterns` function provided by Django, as well as `format_suffix_patterns` you should make sure that the `i18n_patterns` function is applied as the final, or outermost function. For example: + +    url patterns = [ +        … +    ] + +    urlpatterns = i18n_patterns( +        format_suffix_patterns(urlpatterns, allowed=['json', 'html']) +    ) +  ---  ## Accept headers vs. format suffixes diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md index f5bbdfdd..6374e305 100755 --- a/docs/api-guide/generic-views.md +++ b/docs/api-guide/generic-views.md @@ -214,6 +214,8 @@ You won't typically need to override the following methods, although you might n  The mixin classes provide the actions that are used to provide the basic view behavior.  Note that the mixin classes provide action methods rather than defining the handler methods, such as `.get()` and `.post()`, directly.  This allows for more flexible composition of behavior. +The mixin classes can be imported from `rest_framework.mixins`. +  ## ListModelMixin  Provides a `.list(request, *args, **kwargs)` method, that implements listing a queryset. @@ -258,6 +260,8 @@ If an object is deleted this returns a `204 No Content` response, otherwise it w  The following classes are the concrete generic views.  If you're using generic views this is normally the level you'll be working at unless you need heavily customized behavior. +The view classes can be imported from `rest_framework.generics`. +  ## CreateAPIView  Used for **create-only** endpoints. diff --git a/docs/api-guide/relations.md b/docs/api-guide/relations.md index e56db229..50e3b7b5 100644 --- a/docs/api-guide/relations.md +++ b/docs/api-guide/relations.md @@ -231,6 +231,7 @@ This field is always read-only.  * `view_name` - The view name that should be used as the target of the relationship.  If you're using [the standard router classes][routers] this will be a string with the format `<model_name>-detail`.  **required**.  * `lookup_field` - The field on the target that should be used for the lookup.  Should correspond to a URL keyword argument on the referenced view.  Default is `'pk'`. +* `lookup_url_kwarg` - The name of the keyword argument defined in the URL conf that corresponds to the lookup field. Defaults to using the same value as `lookup_field`.  * `format` - If using format suffixes, hyperlinked fields will use the same format suffix for the target unless overridden by using the `format` argument.  --- diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md index 929a1710..3a8a8f6c 100644 --- a/docs/api-guide/routers.md +++ b/docs/api-guide/routers.md @@ -49,6 +49,38 @@ This means you'll need to explicitly set the `base_name` argument when registeri  --- +### Using `include` with routers + +The `.urls` attribute on a router instance is simply a standard list of URL patterns. There are a number of different styles for how you can include these URLs. + +For example, you can append `router.urls` to a list of existing views… + +    router = routers.SimpleRouter() +    router.register(r'users', UserViewSet) +    router.register(r'accounts', AccountViewSet) +     +    urlpatterns = [ +        url(r'^forgot-password/$, ForgotPasswordFormView.as_view(), +    ] +     +    urlpatterns += router.urls + +Alternatively you can use Django's `include` function, like so… + +    urlpatterns = [ +        url(r'^forgot-password/$, ForgotPasswordFormView.as_view(), +        url(r'^', include(router.urls)) +    ] + +Router URL patterns can also be namespaces. + +    urlpatterns = [ +        url(r'^forgot-password/$, ForgotPasswordFormView.as_view(), +        url(r'^api/', include(router.urls, namespace='api')) +    ] + +If using namespacing with hyperlinked serializers you'll also need to ensure that any `view_name` parameters on the serializers correctly reflect the namespace. In the example above you'd need to include a parameter such as `view_name='api:user-detail'` for serializer fields hyperlinked to the user detail view. +  ### Extra link and actions  Any methods on the viewset decorated with `@detail_route` or `@list_route` will also be routed. @@ -68,6 +100,24 @@ The following URL pattern would additionally be generated:  * URL pattern: `^users/{pk}/set_password/$`  Name: `'user-set-password'` +If you do not want to use the default URL generated for your custom action, you can instead use the url_path parameter to customize it. + +For example, if you want to change the URL for our custom action to `^users/{pk}/change-password/$`, you could write: + +    from myapp.permissions import IsAdminOrIsSelf +    from rest_framework.decorators import detail_route +     +    class UserViewSet(ModelViewSet): +        ... +         +        @detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf], url_path='change-password') +        def set_password(self, request, pk=None): +            ... + +The above example would now generate the following URL pattern: + +* URL pattern: `^users/{pk}/change-password/$`  Name: `'user-change-password'` +  For more information see the viewset documentation on [marking extra actions for routing][route-decorators].  # API Guide diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index dcbbd5f2..9a9d5032 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -384,7 +384,7 @@ This manager class now more nicely encapsulates that user instances and profile              has_support_contract=validated_data['profile']['has_support_contract']          ) -For more details on this approach see the Django documentation on [model managers](model-managers), and [this blogpost on using model and manger classes](encapsulation-blogpost). +For more details on this approach see the Django documentation on [model managers](model-managers), and [this blogpost on using model and manager classes](encapsulation-blogpost).  ## Dealing with multiple objects | 
