Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#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 Tim Graham, 9 years ago

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?

comment:2 by Johannes Maron, 9 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 Tim Graham, 9 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 Johannes Maron, 9 years ago

Maybe it's only me. So a line more of documentation seems to be good enough.

comment:5 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: newclosed

In 2c4c67af:

Fixed #26514 -- Documented that User.refresh_from_db() doesn't clear the permission cache.

comment:6 by Tim Graham, 9 years ago

Component: contrib.authDocumentation
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

comment:7 by Tim Graham <timograham@…>, 9 years ago

In 12606d2:

[1.9.x] Fixed #26514 -- Documented that User.refresh_from_db() doesn't clear the permission cache.

Backport of 2c4c67af94318b15df7d9d37b936e07e8168bc73 from master

comment:8 by Johannes Maron, 9 years ago

Thanks :)

Note: See TracTickets for help on using tickets.
Back to Top