Skip to content

Commit aecca9d

Browse files
carltongibsonCarlton Gibson
authored andcommitted
Add note on force_authenticate + refresh_from_db
…in case you’re reusing the same in-memory user whilst updating it in the DB. Closes #5016, closes #5066, closes #4102
1 parent e29ad1e commit aecca9d

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

docs/api-guide/testing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ For example, when forcibly authenticating using a token, you might do something
8686

8787
---
8888

89+
**Note**: `force_authenticate` directly sets `request.user` to the in-memory `user` instance. If you are re-using the same `user` instance across multiple tests that update the saved `user` state, you may need to call [`refresh_from_db()`][refresh_from_db_docs] between tests.
90+
91+
---
92+
8993
**Note**: When using `APIRequestFactory`, the object that is returned is Django's standard `HttpRequest`, and not REST framework's `Request` object, which is only generated once the view is called.
9094

9195
This means that setting attributes directly on the request object may not always have the effect you expect. For example, setting `.token` directly will have no effect, and setting `.user` directly will only work if session authentication is being used.
@@ -378,3 +382,4 @@ For example, to add support for using `format='html'` in test requests, you migh
378382
[client]: https://docs.djangoproject.com/en/stable/topics/testing/tools/#the-test-client
379383
[requestfactory]: https://docs.djangoproject.com/en/stable/topics/testing/advanced/#django.test.client.RequestFactory
380384
[configuration]: #configuration
385+
[refresh_from_db_docs]: https://docs.djangoproject.com/en/1.11/ref/models/instances/#django.db.models.Model.refresh_from_db

0 commit comments

Comments
 (0)