Skip to content

Commit 7933f91

Browse files
[Low Priority] View documentation (ansible#374)
Fixes ansible#371
1 parent 58f85da commit 7933f91

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/lib/views.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,32 @@ class DefaultAPIView(AnsibleBaseView):
4848
all my good stuff
4949
...
5050
```
51+
52+
53+
## Adding extra related fields
54+
55+
There may be cases where the associative router does not provide a way to add specific related views to your view. As a way to help combat this the `AnsibleBaseView` class has a method defined called `extra_related_fields` which returns an empty dict (`{}`) by default. The entries expected in this dictionary are in the format:
56+
```
57+
{
58+
"string": "url",
59+
}
60+
```
61+
Where string is the name of the field under the related section in the API and URL is the URL to that object.
62+
63+
For example, if `extra_related_fields` returned:
64+
```
65+
{
66+
'authenticators': reverse('user-authenticators-list', kwargs={'pk': obj.pk}),
67+
}
68+
```
69+
70+
We would expect to see the related fields for an object to be something along the lines of:
71+
```
72+
"related": {
73+
"authenticators": "/api/gateway/v1/users/1/authenticators/"
74+
}
75+
```
76+
77+
The URL for that entry, in our example, will be generated from the reverse lookup user-authenticators-list url and should end up as something like `/api/users/:id/authenticators/` where :id comes from the `obj.pk`.
78+
79+
This can be particularly useful if a feature in DAB wants to extend a view that is created by a service. In this case you can create a MixIn like `ansible_base.oauth2_provider.views.user_mixin.DABOAuth2UserViewsetMixin` which can set `extra_related_fields` and then have the view in the service extend the Mixin.

0 commit comments

Comments
 (0)