Skip to content

Commit 29f84aa

Browse files
committed
Merge pull request #3831 from ticosax/doc-tweak-admin-authtoken
Document how to tweak authtoken.admin.TokenAdmin
2 parents 01b1d10 + 717e27c commit 29f84aa

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

docs/api-guide/authentication.md

+15
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ The `curl` command line tool may be useful for testing token authenticated APIs.
165165

166166
#### Generating Tokens
167167

168+
##### By using signals
169+
168170
If you want every user to have an automatically generated Token, you can simply catch the User's `post_save` signal.
169171

170172
from django.conf import settings
@@ -187,6 +189,8 @@ If you've already created some users, you can generate tokens for all existing u
187189
for user in User.objects.all():
188190
Token.objects.get_or_create(user=user)
189191

192+
##### By exposing an api endpoint
193+
190194
When using `TokenAuthentication`, you may want to provide a mechanism for clients to obtain a token given the username and password. REST framework provides a built-in view to provide this behavior. To use it, add the `obtain_auth_token` view to your URLconf:
191195

192196
from rest_framework.authtoken import views
@@ -202,6 +206,17 @@ The `obtain_auth_token` view will return a JSON response when valid `username` a
202206

203207
Note that the default `obtain_auth_token` view explicitly uses JSON requests and responses, rather than using default renderer and parser classes in your settings. If you need a customized version of the `obtain_auth_token` view, you can do so by overriding the `ObtainAuthToken` view class, and using that in your url conf instead.
204208

209+
##### With Django admin
210+
211+
It is also possible to create Tokens manually through admin interface. In case you are using a large user base, we recommend that you monkey patch the `TokenAdmin` class to customize it to your needs, more specifically by declaring the `user` field as `raw_field`.
212+
213+
`your_app/admin.py`:
214+
215+
from rest_framework.authtoken.admin import TokenAdmin
216+
217+
TokenAdmin.raw_id_fields = ('user',)
218+
219+
205220
#### Schema migrations
206221

207222
The `rest_framework.authtoken` app includes both Django native migrations (for Django versions >1.7) and South migrations (for Django versions <1.7) that will create the authtoken table.

0 commit comments

Comments
 (0)