#34822 closed New feature (fixed)
Provide migration serializer for `_lru_cache_wrapper`
| Reported by: | Natalia Bidart | Owned by: | Nick Pope |
|---|---|---|---|
| Component: | Migrations | Version: | dev |
| Severity: | Normal | Keywords: | serializer functools cache |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Following ticket #24561, and this forum post, it would be useful to have the migrations serializer knowing how to serialize the _lru_cache_wrapper type which is returned when calling the functools.cache and functools.lru_cache decorators.
An example of a use case would be a callable defined for either model field's choices or default that perform an expensive calculation (either CPI or IO bound), and such callable is wrapped with a caching decorator. For instance:
import functools @functools.cache def fib(n): if n < 2: return n return fib(n - 1) + fib(n - 2) @functools.cache def get_choices(): return {i: fib(i) for i in range(20)} class Spiral(models.Model): size = models.IntegerField(choices=get_choices)
Currently, the error that is raised when generating migrations (assuming support for callable choices), is:
ValueError: Cannot serialize: <functools._lru_cache_wrapper object at 0x7f819ab3b270>
Lastly, while I do appreciate this could be done with a custom serializer, I think it would be of value to provide this in Django core since using the mentioned the decorators is not (IMHO) a niche case.
Change History (9)
comment:1 by , 2 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:4 by , 2 years ago
| Patch needs improvement: | set |
|---|
Settings as patch needs improvement due to minor issues with the docs.
comment:5 by , 2 years ago
| Patch needs improvement: | unset |
|---|
comment:6 by , 2 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Tentatively accepted, let's see how complicated it will be.