Do not access the session unless needed #509
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If the session wasn't accessed we do not need to modify the response.
Session get accessed lazily if someone accesses request.user or
request.session. In those cases Django will add cookie to the
Vary-On HTTP header. This will prevent browsers from caching the
results.
Furthermore, accessing the user when not needed will cause unwanted
IO on request, that don't need authentication or vary content based
on users.
We use the same behavior Django implements in the authentication
middlware, where a user is a lazy object and will only be fetched
if needed. We simply overwrite the user and add another setup
hook.