From f5f23793e34324552f323725fa25f09b34380acc Mon Sep 17 00:00:00 2001 From: Rudolf Olah Date: Thu, 27 Jun 2013 16:30:24 -0400 Subject: #955 updated documentation for overriding `routes` attribute in Router sub-classes --- docs/api-guide/routers.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'docs/api-guide') diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md index b74b6e13..feff0fbf 100644 --- a/docs/api-guide/routers.md +++ b/docs/api-guide/routers.md @@ -98,7 +98,7 @@ As with `SimpleRouter` the trailing slashs on the URL routes can be removed by s Implementing a custom router isn't something you'd need to do very often, but it can be useful if you have specific requirements about how the your URLs for your API are strutured. Doing so allows you to encapsulate the URL structure in a reusable way that ensures you don't have to write your URL patterns explicitly for each new view. -The simplest way to implement a custom router is to subclass one of the existing router classes. The `.routes` attribute is used to template the URL patterns that will be mapped to each viewset. +The simplest way to implement a custom router is to subclass one of the existing router classes. The `.routes` attribute is used to template the URL patterns that will be mapped to each viewset. The `.routes` attribute is a list of `Route` named tuples. ## Example @@ -109,10 +109,18 @@ The following example will only route to the `list` and `retrieve` actions, and A router for read-only APIs, which doesn't use trailing suffixes. """ routes = [ - (r'^{prefix}$', {'get': 'list'}, '{basename}-list'), - (r'^{prefix}/{lookup}$', {'get': 'retrieve'}, '{basename}-detail') + Route(url=r'^{prefix}$', + mapping={'get': 'list'}, + name='{basename}-list', + initkwargs={}), + Route(url=r'^{prefix}/{lookup}$', + mapping={'get': 'retrieve'}, + name='{basename}-detail', + initkwargs={}) ] +The `SimpleRouter` class provides another example of setting the `.routes` attribute. + ## Advanced custom routers If you want to provide totally custom behavior, you can override `BaseRouter` and override the `get_urls(self)` method. The method should insect the registered viewsets and return a list of URL patterns. The registered prefix, viewset and basename tuples may be inspected by accessing the `self.registry` attribute. -- cgit v1.2.3 From 53dc98eefb5644e60495ca86c7424e669d0a86f1 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 1 Jul 2013 17:22:42 +0100 Subject: Added Django OAuth2 Consumer package --- docs/api-guide/authentication.md | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'docs/api-guide') diff --git a/docs/api-guide/authentication.md b/docs/api-guide/authentication.md index 8cf995b3..22c3297c 100755 --- a/docs/api-guide/authentication.md +++ b/docs/api-guide/authentication.md @@ -355,6 +355,10 @@ HTTP digest authentication is a widely implemented scheme that was intended to r The [Django OAuth Toolkit][django-oauth-toolkit] package provides OAuth 2.0 support, and works with Python 2.7 and Python 3.3+. The package is maintained by [Evonove][evonove] and uses the excelllent [OAuthLib][oauthlib]. The package is well documented, and comes as a recommended alternative for OAuth 2.0 support. +## Django OAuth2 Consumer + +The [Django Oauth2 Consumer][doac] library from [Rediker Software][rediker] is another package that provides [OAuth2 support for REST framework][doac-rest-framework]. The package includes token scoping permissions on tokens, which allows finer-grained access to your API. + [cite]: http://jacobian.org/writing/rest-worst-practices/ [http401]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2 [http403]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4 @@ -376,3 +380,6 @@ The [Django OAuth Toolkit][django-oauth-toolkit] package provides OAuth 2.0 supp [django-oauth-toolkit]: https://github.com/evonove/django-oauth-toolkit [evonove]: https://github.com/evonove/ [oauthlib]: https://github.com/idan/oauthlib +[doac]: https://github.com/Rediker-Software/doac +[rediker]: https://github.com/Rediker-Software +[doac-rest-framework]: https://github.com/Rediker-Software/doac/blob/master/docs/markdown/integrations.md -- cgit v1.2.3 From 8274ff7d9c692d27f926af7610a5a547ced09a2e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 1 Jul 2013 17:27:23 +0100 Subject: Capitalization on OAuth --- docs/api-guide/authentication.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/api-guide') diff --git a/docs/api-guide/authentication.md b/docs/api-guide/authentication.md index 22c3297c..768f156b 100755 --- a/docs/api-guide/authentication.md +++ b/docs/api-guide/authentication.md @@ -357,7 +357,7 @@ The [Django OAuth Toolkit][django-oauth-toolkit] package provides OAuth 2.0 supp ## Django OAuth2 Consumer -The [Django Oauth2 Consumer][doac] library from [Rediker Software][rediker] is another package that provides [OAuth2 support for REST framework][doac-rest-framework]. The package includes token scoping permissions on tokens, which allows finer-grained access to your API. +The [Django OAuth2 Consumer][doac] library from [Rediker Software][rediker] is another package that provides [OAuth2 support for REST framework][doac-rest-framework]. The package includes token scoping permissions on tokens, which allows finer-grained access to your API. [cite]: http://jacobian.org/writing/rest-worst-practices/ [http401]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2 -- cgit v1.2.3 From 8d410c4671fd4596089883e360f5d3e8f9e0f62b Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 1 Jul 2013 17:32:06 +0100 Subject: Tweak text --- docs/api-guide/authentication.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/api-guide') diff --git a/docs/api-guide/authentication.md b/docs/api-guide/authentication.md index 768f156b..390fba8c 100755 --- a/docs/api-guide/authentication.md +++ b/docs/api-guide/authentication.md @@ -357,7 +357,7 @@ The [Django OAuth Toolkit][django-oauth-toolkit] package provides OAuth 2.0 supp ## Django OAuth2 Consumer -The [Django OAuth2 Consumer][doac] library from [Rediker Software][rediker] is another package that provides [OAuth2 support for REST framework][doac-rest-framework]. The package includes token scoping permissions on tokens, which allows finer-grained access to your API. +The [Django OAuth2 Consumer][doac] library from [Rediker Software][rediker] is another package that provides [OAuth 2.0 support for REST framework][doac-rest-framework]. The package includes token scoping permissions on tokens, which allows finer-grained access to your API. [cite]: http://jacobian.org/writing/rest-worst-practices/ [http401]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2 -- cgit v1.2.3 From e7529b4072274797daf5b886dbac4c0e65a65674 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 2 Jul 2013 16:22:22 +0100 Subject: Fix broken link by hacking around md->html translating --- docs/api-guide/authentication.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/api-guide') diff --git a/docs/api-guide/authentication.md b/docs/api-guide/authentication.md index 390fba8c..5d6e0d91 100755 --- a/docs/api-guide/authentication.md +++ b/docs/api-guide/authentication.md @@ -382,4 +382,4 @@ The [Django OAuth2 Consumer][doac] library from [Rediker Software][rediker] is a [oauthlib]: https://github.com/idan/oauthlib [doac]: https://github.com/Rediker-Software/doac [rediker]: https://github.com/Rediker-Software -[doac-rest-framework]: https://github.com/Rediker-Software/doac/blob/master/docs/markdown/integrations.md +[doac-rest-framework]: https://github.com/Rediker-Software/doac/blob/master/docs/markdown/integrations.md# -- cgit v1.2.3 From e460180a4dd86ff74cc786aabfeeba1c31d17413 Mon Sep 17 00:00:00 2001 From: Rudolf Olah Date: Tue, 2 Jul 2013 13:20:25 -0400 Subject: #955 updated router docs with more information on the `Route` named tuple and its parameters. --- docs/api-guide/routers.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'docs/api-guide') diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md index feff0fbf..1fb15fae 100644 --- a/docs/api-guide/routers.md +++ b/docs/api-guide/routers.md @@ -100,6 +100,18 @@ Implementing a custom router isn't something you'd need to do very often, but it The simplest way to implement a custom router is to subclass one of the existing router classes. The `.routes` attribute is used to template the URL patterns that will be mapped to each viewset. The `.routes` attribute is a list of `Route` named tuples. +The arguments to the `Route` named tuple are: + +* `url`: The URL to be routed. There are format arguments available, defined in `SimpleRouter.get_urls`: + * `prefix` - The URL prefix to use for this set of routes. + * `lookup` - The lookup field used to match against a single instance. + * `trailing_slash` - the value of `.trailing_slash`. +* `mapping`: Mapping of HTTP method names to the object's methods +* `name`: The name of the URL as used in `reverse` calls. There are format arguments available, defined in `SimpleRouter.get_urls`: + * `basename` - The base to use for the URL names that are created. +* `initkwargs`: Any additional arguments to the view. + * `suffix` - reserved for identifying the viewset type, used when generating the breadcrumb links, e.g. `AccountViewSet` becomes `Account List` when `suffix='List'`. + ## Example The following example will only route to the `list` and `retrieve` actions, and does not use the trailing slash convention. -- cgit v1.2.3 From 6d1c47461879906f5f279da6a9a50900d36c6b4e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 2 Jul 2013 22:29:38 +0100 Subject: Minor tidying --- docs/api-guide/routers.md | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'docs/api-guide') diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md index 1fb15fae..86582905 100644 --- a/docs/api-guide/routers.md +++ b/docs/api-guide/routers.md @@ -102,15 +102,19 @@ The simplest way to implement a custom router is to subclass one of the existing The arguments to the `Route` named tuple are: -* `url`: The URL to be routed. There are format arguments available, defined in `SimpleRouter.get_urls`: - * `prefix` - The URL prefix to use for this set of routes. - * `lookup` - The lookup field used to match against a single instance. - * `trailing_slash` - the value of `.trailing_slash`. -* `mapping`: Mapping of HTTP method names to the object's methods -* `name`: The name of the URL as used in `reverse` calls. There are format arguments available, defined in `SimpleRouter.get_urls`: - * `basename` - The base to use for the URL names that are created. -* `initkwargs`: Any additional arguments to the view. - * `suffix` - reserved for identifying the viewset type, used when generating the breadcrumb links, e.g. `AccountViewSet` becomes `Account List` when `suffix='List'`. +**url**: A string representing the URL to be routed. May include the following format strings: + +* `{prefix}` - The URL prefix to use for this set of routes. +* `{lookup}` - The lookup field used to match against a single instance. +* `{trailing_slash}` - Either a '/' or an empty string, depending on the `trailing_slash` argument. + +**mapping**: A mapping of HTTP method names to the view methods + +**name**: The name of the URL as used in `reverse` calls. May include the following format string: + +* `{basename}` - The base to use for the URL names that are created. + +**initkwargs**: A dictionary of any additional arguments that should be passed when instantiating the view. Note that the `suffix` argument is reserved for identifying the viewset type, used when generating the view name and breadcrumb links. ## Example @@ -118,17 +122,17 @@ The following example will only route to the `list` and `retrieve` actions, and class ReadOnlyRouter(SimpleRouter): """ - A router for read-only APIs, which doesn't use trailing suffixes. + A router for read-only APIs, which doesn't use trailing slashes. """ routes = [ Route(url=r'^{prefix}$', mapping={'get': 'list'}, name='{basename}-list', - initkwargs={}), + initkwargs={'suffix': 'List'}), Route(url=r'^{prefix}/{lookup}$', mapping={'get': 'retrieve'}, name='{basename}-detail', - initkwargs={}) + initkwargs={'suffix': 'Detail'}) ] The `SimpleRouter` class provides another example of setting the `.routes` attribute. -- cgit v1.2.3 From b88bdfb9a5b82fce89a0e8f263e92e0817c9241d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 4 Jul 2013 13:30:56 +0100 Subject: Add section on dynamically modifying fields. Refs #958 --- docs/api-guide/serializers.md | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'docs/api-guide') diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index f8761cb2..dc16ba83 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -423,6 +423,47 @@ You can create customized subclasses of `ModelSerializer` or `HyperlinkedModelSe Doing so should be considered advanced usage, and will only be needed if you have some particular serializer requirements that you often need to repeat. +## Dynamically modifiying fields + +Once a serializer has been initialized, the dictionary of fields that are set on the serializer may be accessed using the `.fields` attribute. Accessing and modifying this attribute allows you to dynamically modify the serializer. + +### Example + +For example, if you wanted to be able to set which fields should be used by a serializer at the point of initializing it, you could create a serializer class like so: + + class DynamicFieldsModelSerializer(serializers.ModelSerializer): + """ + A ModelSerializer that takes an additional `fields` argument that + controls which fields should be displayed. + """ + + def __init__(self, *args, **kwargs): + # Don't pass the 'fields' arg up to the superclass + fields = kwargs.pop('fields', None) + + # Instatiate the superclass normally + super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs) + + if fields: + # Drop any fields that are not specified in the `fields` argument. + allowed = set(fields) + existing = set(self.fields.keys()) + for field_name in existing - allowed: + self.fields.pop(field_name) + +This would then allow you to do the following: + + >>> class UserSerializer(DynamicFieldsModelSerializer): + >>> class Meta: + >>> model = User + >>> fields = ('id', 'username', 'email') + >>> + >>> print UserSerializer(user) + {'id': 2, 'username': 'jonwatts', 'email': 'jon@example.com'} + >>> + >>> print UserSerializer(user, fields=('id', 'email')) + {'id': 2, 'email': 'jon@example.com'} + ## Customising the default fields The `field_mapping` attribute is a dictionary that maps model classes to serializer classes. Overriding the attribute will let you set a different set of default serializer classes. @@ -457,7 +498,7 @@ Note that the `model_field` argument will be `None` for reverse relationships. Returns the field instance that should be used for non-relational, non-pk fields. -## Example +### Example The following custom model serializer could be used as a base class for model serializers that should always exclude the pk by default. -- cgit v1.2.3 From a890116ab31e57af3bd1382c1f17259fa368f988 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 4 Jul 2013 13:49:28 +0100 Subject: Minor docs addition --- docs/api-guide/serializers.md | 2 ++ 1 file changed, 2 insertions(+) (limited to 'docs/api-guide') diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index dc16ba83..8e9de10e 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -427,6 +427,8 @@ Doing so should be considered advanced usage, and will only be needed if you hav Once a serializer has been initialized, the dictionary of fields that are set on the serializer may be accessed using the `.fields` attribute. Accessing and modifying this attribute allows you to dynamically modify the serializer. +Modifying the `fields` argument directly allows you to do interesting things such as changing the arguments on serializer fields at runtime, rather than at the point of declaring the serializer. + ### Example For example, if you wanted to be able to set which fields should be used by a serializer at the point of initializing it, you could create a serializer class like so: -- cgit v1.2.3