diff options
Diffstat (limited to 'docs')
| -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 | ||||
| -rw-r--r-- | docs/index.md | 2 | ||||
| -rw-r--r-- | docs/topics/3.0-announcement.md | 1 | ||||
| -rw-r--r-- | docs/tutorial/1-serialization.md | 4 | ||||
| -rw-r--r-- | docs/tutorial/6-viewsets-and-routers.md | 2 | 
10 files changed, 75 insertions, 5 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 diff --git a/docs/index.md b/docs/index.md index f7b3ed2a..7ccec12f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -231,7 +231,7 @@ Send a description of the issue via email to [rest-framework-security@googlegrou  ## License -Copyright (c) 2011-2014, Tom Christie +Copyright (c) 2011-2015, Tom Christie  All rights reserved.  Redistribution and use in source and binary forms, with or without diff --git a/docs/topics/3.0-announcement.md b/docs/topics/3.0-announcement.md index 0710766f..68d24782 100644 --- a/docs/topics/3.0-announcement.md +++ b/docs/topics/3.0-announcement.md @@ -940,6 +940,7 @@ The default JSON renderer will return float objects for un-coerced `Decimal` ins  * The serializer `ChoiceField` does not currently display nested choices, as was the case in 2.4. This will be address as part of 3.1.  * Due to the new templated form rendering, the 'widget' option is no longer valid. This means there's no easy way of using third party "autocomplete" widgets for rendering select inputs that contain a large number of choices. You'll either need to use a regular select or a plain text input. We may consider addressing this in 3.1 or 3.2 if there's sufficient demand.  * Some of the default validation error messages were rewritten and might no longer be pre-translated. You can still [create language files with Django][django-localization] if you wish to localize them. +* `APIException` subclasses could previously take could previously take any arbitrary type in the `detail` argument. These exceptions now use translatable text strings, and as a result call `force_text` on the `detail` argument, which *must be a string*. If you need complex arguments to an `APIException` class, you should subclass it and override the `__init__()` method. Typically you'll instead want to use a custom exception handler to provide for non-standard error responses.  --- diff --git a/docs/tutorial/1-serialization.md b/docs/tutorial/1-serialization.md index ff507a2b..60a3d989 100644 --- a/docs/tutorial/1-serialization.md +++ b/docs/tutorial/1-serialization.md @@ -124,7 +124,7 @@ The first part of the serializer class defines the fields that get serialized/de  A serializer class is very similar to a Django `Form` class, and includes similar validation flags on the various fields, such as `required`, `max_length` and `default`. -The field flags can also control how the serializer should be displayed in certain circumstances, such as when rendering to HTML. The `style={'type': 'textarea'}` flag above is equivelent to using `widget=widgets.Textarea` on a Django `Form` class. This is particularly useful for controlling how the browsable API should be displayed, as we'll see later in the tutorial. +The field flags can also control how the serializer should be displayed in certain circumstances, such as when rendering to HTML. The `{'base_template': 'textarea.html'}` flag above is equivelent to using `widget=widgets.Textarea` on a Django `Form` class. This is particularly useful for controlling how the browsable API should be displayed, as we'll see later in the tutorial.  We can actually also save ourselves some time by using the `ModelSerializer` class, as we'll see later, but for now we'll keep our serializer definition explicit. @@ -206,7 +206,7 @@ One nice property that serializers have is that you can inspect all the fields i      SnippetSerializer():          id = IntegerField(label='ID', read_only=True)          title = CharField(allow_blank=True, max_length=100, required=False) -        code = CharField(style={'type': 'textarea'}) +        code = CharField(style={'base_template': 'textarea.html'})          linenos = BooleanField(required=False)          language = ChoiceField(choices=[('Clipper', 'FoxPro'), ('Cucumber', 'Gherkin'), ('RobotFramework', 'RobotFramework'), ('abap', 'ABAP'), ('ada', 'Ada')...          style = ChoiceField(choices=[('autumn', 'autumn'), ('borland', 'borland'), ('bw', 'bw'), ('colorful', 'colorful')... diff --git a/docs/tutorial/6-viewsets-and-routers.md b/docs/tutorial/6-viewsets-and-routers.md index d55a60de..63dff73f 100644 --- a/docs/tutorial/6-viewsets-and-routers.md +++ b/docs/tutorial/6-viewsets-and-routers.md @@ -53,6 +53,8 @@ Notice that we've also used the `@detail_route` decorator to create a custom act  Custom actions which use the `@detail_route` decorator will respond to `GET` requests.  We can use the `methods` argument if we wanted an action that responded to `POST` requests. +The URLs for custom actions by default depend on the method name itself. If you want to change the way url should be constructed, you can include url_path as a decorator keyword argument. +  ## Binding ViewSets to URLs explicitly  The handler methods only get bound to the actions when we define the URLConf. | 
