Opened 7 years ago
Closed 7 years ago
#29911 closed New feature (needsinfo)
Standardize delete_cached_value API on model Fields
| Reported by: | Taylor H | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 2.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I created a custom Django model field that is expensive to calculate and I store a cached value on the model. I am not able to clear the cache when some uses the refresh_from_db() method though, since there is no standard API to hook into it. It appears that there is a custom behavior for ForeignKey that calls the delete_cached_value function, if it detects it's a ForeignKey
https://github.com/django/django/blob/master/django/db/models/base.py#L569
It would be great to allow any custom Field to implement delete_cached_value and have it called inside refresh_from_db. What are your thoughts? I would be happy to submit a PR if this seems reasonable
Change History (5)
comment:1 by , 7 years ago
| Description: | modified (diff) |
|---|---|
| Summary: | Standardize delete_cached_value API on model Fields → Standardize `delete_cached_value API` on model Fields |
comment:2 by , 7 years ago
| Summary: | Standardize `delete_cached_value API` on model Fields → Standardize delete_cached_value API on model Fields |
|---|
comment:3 by , 7 years ago
comment:4 by , 7 years ago
Comment from Carlton Gibson: "My initial thought here is just to override refresh_from_db(). I appreciate that’s slightly more involved than just using the field on the model, but it does mean no extra API in Django. "
comment:5 by , 7 years ago
| Resolution: | → needsinfo |
|---|---|
| Status: | new → closed |
Hi taylor, I'm going to close this as-is. If you can follow-up with answers to the questions we may re-open. Thanks.
Could you detail a bit more about what your use case looks like? It's not clear to me what changes in Django are needed to solve it.