#26514 closed Cleanup/optimization (fixed)
User.refresh_from_db() does not clear Permission cache.
| Reported by: | Johannes Maron | Owned by: | nobody |
|---|---|---|---|
| Component: | Documentation | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
User.refresh_from_db() does not clear the Permission cache.
ModelBackend implements a cache for the Permission. This cache is not cleared or rebuild when calling refresh_from_db().
I find this very confusing and somewhat unexpected behavior. This should ether be documented or better the cache gets flushed when refresh_from_db() is called.
Change History (8)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
I think the refresh_from_db documentation is fine. It's the permissions cache that could be better documented and maybe even extended.
It is somewhat unexpected, that you can add or set User.user_permissions but that this will have no effect unless you allocate a new instance of User.
Strictly speaking an m2m relationship is called a "field" in Django, or is it not? Since the permissions are depended on such a relation, I naturally presumed the permissions cache would be covered.
It would be a good thing to have a public API to clear the permissions cache. As a plus it would be great if the cache handles invalidation itself.
comment:3 by , 10 years ago
The ModelBackend cache is documented. I suppose it wouldn't hurt to add a sentence there like "refresh_from_db() won't clear this cache."
The value of user_permissions isn't actually cached. The cache is stored in a private attribute user._per_cache. I'm weary of heading down the road of adding custom refresh_from_db() methods to all models that have similar behavior.
comment:4 by , 10 years ago
Maybe it's only me. So a line more of documentation seems to be good enough.
comment:6 by , 10 years ago
| Component: | contrib.auth → Documentation |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
The refresh_from_db() documentation says, "Note that only fields of the model are reloaded from the database. Other database dependent values such as annotations are not reloaded."
I don't find this unclear, but perhaps you'd like to suggest some clarification?